sistema_progs

Programas para customizar o meu entorno de traballo nos meus equipos persoais
Log | Files | Refs

D1522.large-array-passing.sh (3507B)


      1 #!/bin/bash
      2 
      3 # 一旦配列に入れるだけでもコストになっている。
      4 # time { a=({000000..500000}); printf '%s\0' "${a[@]}"; } >/dev/null
      5 # time { printf '%s\0' {000000..500000}; } >/dev/null
      6 
      7 if ((_ble_bash>=50000)); then
      8   a=({000000..500000})
      9 else
     10   a=({000000..050000})
     11 fi
     12 echo a:initialized
     13 
     14 # ble-measure 'declare -p a > .tmp' # 100ms
     15 
     16 function qprint1 {
     17   printf '%s\n' "${a[@]@Q}"
     18 }
     19 function qprint1b {
     20   printf '%q\n' "${a[@]}"
     21 }
     22 function qprint2 {
     23   local i n=${#a[@]}
     24   for ((i=0;i<n;i++)); do
     25     echo "${a[i]@Q}"
     26   done
     27 }
     28 #ble-measure 'qprint1 > .tmp' # 1573
     29 #ble-measure 'qprint1b > .tmp' # 1285
     30 #ble-measure 'qprint2 > .tmp' #2409
     31 
     32 function eval1 {
     33   eval "b=($(< .tmp))"
     34 }
     35 #ble-measure 'eval1' # 1073
     36 
     37 #------------------------------------------------------------------------------
     38 
     39 function zprint0 {
     40   # bash-3.* では ^A, ^? が化けるので補正が必要
     41   printf '%s\0' "${a[@]}" > .tmp
     42 }
     43 function zprint1 {
     44   local i n=${#a[@]}
     45   for ((i=0;i<n;i++)); do
     46     printf '%s\0' "${a[i]}"
     47   done
     48 }
     49 #ble-measure zprint0 # 954
     50 #ble-measure 'zprint1 > .tmp' # 2303
     51 
     52 function zmapfile1 {
     53   mapfile -d '' b < .tmp
     54 }
     55 #ble-measure 'zmapfile1' # 1603
     56 
     57 # ble-measure 'ble/util/writearray -d "" a > .tmp' # 883ms
     58 # ble-measure 'ble/util/readarray -d "" b < .tmp' # 1636ms
     59 # echo "a:${#a[@]} -> b:${#b[@]}"
     60 
     61 #------------------------------------------------------------------------------
     62 
     63 function save-nl1 {
     64   printf '%s\n' "${a[@]}"
     65 }
     66 function save-nl2 {
     67   local i n=${#a[@]}
     68   for ((i=0;i<n;i++)); do
     69     echo "${a[i]}"
     70   done
     71 }
     72 function save-nl3 {
     73   local i n=${#a[@]} B=$1
     74   for ((i=0;i<n;i+=B)); do
     75     printf '%s\n' "${a[@]:i:B}"
     76   done
     77 }
     78 #ble-measure 'save-nl1 > .tmp' # 1129
     79 #ble-measure 'save-nl2 > .tmp' # 2171
     80 #ble-measure 'save-nl3 10 > .tmp' # -
     81 #ble-measure 'save-nl3 100 > .tmp' # -
     82 #ble-measure 'save-nl3 1000 > .tmp' # -
     83 #ble-measure 'save-nl3 10000 > .tmp' # 13948
     84 #ble-measure 'save-nl3 100000 > .tmp' # 3254
     85 
     86 function load-nl1 {
     87   mapfile -t b < .tmp
     88 }
     89 #ble-measure 'mapfile1' # 67
     90 
     91 function save-nlfix1 {
     92   local i n=${#a[@]} ret
     93   for ((i=0;i<n;i++)); do
     94     if [[ ${a[i]} == *$'\n'* ]]; then
     95       ble/string#escape-for-bash-escape-string "${a[i]}"
     96       echo "$ret"
     97       echo "$i" >> .tmp-nlfix
     98     else
     99       echo "${a[i]}"
    100     fi
    101   done
    102 }
    103 function save-nlfix2 {
    104   local i n=${#a[@]} v ret
    105   for ((i=0;i<n;i++)); do
    106     v=${a[i]}
    107     if [[ $v == *$'\n'* ]]; then
    108       ble/string#escape-for-bash-escape-string "$v"
    109       echo "\$'$ret'"
    110       echo "$i" >> .tmp-nlfix
    111     else
    112       echo "$v"
    113     fi
    114   done
    115 }
    116 
    117 function save-nlfix3 {
    118   ble/util/writearray --nlfix a
    119 }
    120 
    121 #ble-measure 'save-nlfix1 > .tmp' # 3157
    122 #ble-measure 'save-nlfix2 > .tmp' # 3119
    123 #ble-measure 'save-nlfix3 > .tmp'
    124 
    125 # 制御文字を含む時: 2382 (gawk), 1284 (nawk), 817 (mawk)
    126 # 制御文字を含まない時: 2150 (gawk), 372 (nawk), 1350 (mawk)
    127 #ble-measure 'save-nlfix3 > .tmp'
    128 
    129 function load-nlfix1 {
    130   mapfile -t b < .tmp
    131   local ifix
    132   for ifix in $(< .tmp-nlfix); do
    133     eval "b[ifix]=${b[ifix]}"
    134   done
    135 }
    136 #ble-measure 'load-nlfix1' # 83
    137 
    138 ble-measure 'ble/util/writearray --nlfix a > .tmp' # 364ms (480ms bash-4.0)
    139 ble-measure 'ble/util/readarray --nlfix b < .tmp' # 81ms (110ms bash-4.0)
    140 echo "a:${#a[@]} -> b:${#b[@]}"
    141 
    142 #------------------------------------------------------------------------------
    143 # function ble/util/save-large-array {
    144 #   ble/util/writearray --nlfix > .tmp
    145 # }
    146 # function ble/util/load-large-array {
    147 #   ble/util/writearray --nlfix < .tmp
    148 # }
    149 
    150 
    151 
    152 a=() b=()