sistema_progs

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

201809-test.sh (5590B)


      1 #!/usr/bin/env bash
      2 
      3 if [[ ! ${BLE_VERSION-} ]]; then
      4   source ../../src/benchmark.sh
      5 fi
      6 
      7 function line {
      8   echo ---------- ---------- ---------- ----------
      9 }
     10 
     11 _ble_util_assign_base="/dev/shm/$UID.$$.read-stdout.tmp"
     12 function veval {
     13   eval "${@:2}" >| "$_ble_util_assign_base"
     14   local _ret="$?"
     15   IFS= read -r -d '' "$1" < "$_ble_util_assign_base"
     16   return "$_ret"
     17 }
     18 
     19 d=/hello/world
     20 dir=/hello/world
     21 dirverylonglong=/hello/world
     22 
     23 # # test
     24 # ble-measure '[ a ]'
     25 # ble-measure 'test a'
     26 # ble-measure '[[ a ]]'
     27 
     28 # # quote globpat
     29 # ble-measure '[[ $dir == / ]]'
     30 # ble-measure '[[ $dir == '\''/'\'' ]]'
     31 # ble-measure '[[ $dir == "/" ]]'
     32 
     33 # # quote var
     34 # ble-measure '[[ $dir == / ]]'
     35 # ble-measure '[[ "$dir" == / ]]'
     36 
     37 # # varname
     38 # ble-measure '[[ $dirverylonglong == / ]]'
     39 # ble-measure '[[ $d == / ]]'
     40 # ble-measure '[[ $dir == / ]]'
     41 
     42 # # startsWith
     43 # ble-measure '[[ $d == /* ]]'
     44 # ble-measure '[[ ${d::1} == / ]]'
     45 # ble-measure '[[ ! ${d##/*} ]]'
     46 # ble-measure '[[ $d =~ ^/ ]]'
     47 
     48 # # contains
     49 # ble-measure '[[ $d == */* ]]'
     50 # ble-measure '[[ ! ${d##*/*} ]]'
     51 # ble-measure '[[ $d =~ / ]]'
     52 
     53 
     54 #------------------------------------------------------------------------------
     55 # 変数の代入
     56 
     57 # # quote rhs
     58 # ble-measure 'd=$d'
     59 # ble-measure 'd="$d"'
     60 
     61 # # multiple assign
     62 # ble-measure 'a=$d b=$d'
     63 # ble-measure 'a=$d; b=$d'
     64 
     65 #------------------------------------------------------------------------------
     66 # 算術式
     67 
     68 # # assign
     69 # ble-measure 'a=0'
     70 # ble-measure '((a=0))'
     71 # ble-measure 'a=0 b=0'
     72 # ble-measure '((a=0,b=0))'
     73 
     74 a=0 b=1
     75 
     76 # # 判定
     77 # ble-measure '[[ a == 0 ]] && x=1'
     78 # ble-measure '((a==0)) && x=1'
     79 # ble-measure '[[ a -eq 0 ]] && x=1'
     80 
     81 # # increment
     82 # ble-measure '((a++))'
     83 # ble-measure "(('a++'))"
     84 # ble-measure 'a=$((a+1))'
     85 # ble-measure 'let a++'
     86 # ble-measure "let 'a++'"
     87 
     88 # # branch1
     89 # ble-measure "((a==0&&(x=1)))"
     90 # ble-measure "((a==0)) && x=1"
     91 # ble-measure "((a==0)) && ((x=1))"
     92 # ble-measure "if ((a==0)); then ((x=1)); fi"
     93 # ble-measure "((a==1&&(x=1)))"
     94 # ble-measure "((a==1)) && ((x=1))"
     95 # ble-measure "if ((a==1)); then ((x=1)); fi"
     96 
     97 # # branch2
     98 # ble-measure "if ((a==0)); then ((x=1)); else ((y=1)); fi"
     99 # ble-measure "((a==0?(x=1):(y=1)))"
    100 # ble-measure "if ((a==1)); then ((x=1)); else ((y=1)); fi"
    101 # ble-measure "((a==1?(x=1):(y=1)))"
    102 
    103 # # branch3
    104 # ble-measure "((a==1?(x=1):(a==2?(x=8):(a==3?(x=4):(x=3)))))"
    105 # ble-measure "((x=a==1?1:(a==2?8:(a==3?4:3))))"
    106 # ble-measure "if ((a==1)); then ((x=1)); elif ((a==2)); then ((x=8)); elif ((a==3)); then ((x=4)); else ((x=3)); fi"
    107 # ble-measure "if [[ a == 1 ]]; then x=1; elif [[ a == 2 ]]; then x=8; elif [[ a == 3 ]]; then x=4; else x=3; fi"
    108 
    109 # ble-measure 'n=0; for i in {1..10000}; do ((n+=i)); done; echo $n'
    110 # ble-measure 'n=0; for i in $(seq 10000); do n=$(echo $n + $i | bc); done; echo $n'
    111 # ble-measure 'for i in $(seq 10000); do printf "$i + "; [ $i -eq 10000 ] && printf "0\n"; done | bc'
    112 # ble-measure "printf -v A '%d + ' {1..10000}; echo \$((\${A}0))"
    113 
    114 #------------------------------------------------------------------------------
    115 # 制御構造
    116 
    117 # # 関数呼出
    118 # function _empty { :; }
    119 # function very_very_long_long_function_name { :; }
    120 # ble-measure :
    121 # ble-measure _empty
    122 # ble-measure very_very_long_long_function_name
    123 
    124 # # ループ
    125 # ble-measure 'a=0; for ((i=0;i<10;i++)); do ((a+=i)); done'
    126 # ble-measure 'a=0; for i in {0..10}; do ((a+=i)); done'
    127 # ble-measure 'a=0; for ((i=0;i<10000;i++)); do ((a+=i)); done'
    128 # ble-measure 'a=0; for i in {0..10000}; do ((a+=i)); done'
    129 
    130 #------------------------------------------------------------------------------
    131 # 配列操作
    132 
    133 a=() b=()
    134 
    135 # # push
    136 # ble-measure "j=0; for i in {0..1000}; do a[j++]=\$i; done"
    137 # ble-measure "for i in {0..1000}; do a+=(\$i); done"
    138 # ble-measure "for i in {0..1000}; do a[\${#a[@]}]=\$i; done"
    139 # ble-measure "for i in {0..1000}; do a=(\"\${a[@]}\" \$i); done"
    140 
    141 #--------------------------------------
    142 # 複数の配列の腹を触ると遅い件
    143 
    144 # #a=({0..1000000})
    145 # for n in 10 20 50 100 200 500 1000 2000 5000 10000 20000 50000 100000 200000; do
    146 #   #a=(); ble-measure "for ((i=0;i<$n;i++)); do ((a[i]=i*i,b[i]=i)); done"
    147 #   #ble-measure "for ((i=0;i<$n;i++)); do ((a[i]=i*i)); done; for ((i=0;i<$n;i++)); do ((b[i]=i)); done"
    148 #   #ble-measure "for ((i=0;i<$n;i++)); do ((a[i]=i*i,b[i]=i)); done"
    149 # done
    150 
    151 # # 配列 & 変数にアクセス → 遅くない
    152 # a=({0..1000000}); ble-measure "x=0; for ((i=0;i<200000;i++)); do ((a[i]=i*i,x=i)); done"
    153 
    154 #------------------------------------------------------------------------------
    155 # 配列 - glob フィルタ
    156 
    157 
    158 # a=({0..999})
    159 # function filter1 { i=0 b=(); for x in "${a[@]}"; do [[ $x == *1?2 ]] && b[i++]=$x; done; }
    160 # function filter2 { veval b 'compgen -X "!*1?2" -W "${a[*]}"'; b=($b); }
    161 # ble-measure 'filter1'
    162 # ble-measure 'filter2'
    163 
    164 # function filter2base { veval b 'type filter2base'; }
    165 # ble-measure 'filter2base'
    166 
    167 #------------------------------------------------------------------------------
    168 # シェルオプション
    169 
    170 # # shopt -q
    171 # function ble/util/has-shopt { [[ :$BASHOPTS: == *:"$1":* ]]; }
    172 # ble-measure 'shopt -q extquote'
    173 # ble-measure '[[ :$BASHOPTS: == *:extquote:* ]]'
    174 # ble-measure 'ble/util/has-shopt extquote'
    175 # ble-measure 'shopt -q extquote &>/dev/null'
    176 
    177 # # [[ -o option-name ]]
    178 # ble-measure '[[ -o hashall ]]'
    179 # ble-measure '[[ $- == *h* ]]'
    180 # ble-measure '[[ :$SHELLOPTS: == *:hashall:* ]]'
    181 
    182 #------------------------------------------------------------------------------
    183 # その他 (未整理)
    184 
    185 # ヒアストリングはそんなに速くない (fork に較べれば格段に速いが)
    186 ble-measure ': <<< hello'