D1302.benchmark-array-copy.sh (6932B)
1 #!/bin/bash 2 3 source src/benchmark.sh 4 5 # 然し計測していて気付いたが実はこれは実際の処理よりも引数の展開の方に 6 # 時間がかかっている様である。と思ったが違う…。これは arr1 の初期化に 7 # 時間がかかっているのだ。 8 arr1=({0..10000}) 9 arr2=() 10 11 #------------------------------------------------------------------------------ 12 # 配列コピー 13 14 #ble-measure 'arr2=("${arr1[@]}")' 15 16 # この結果を見ると bash-4.4 が特別に遅いというよりは bash-5.0 が速い 17 18 # 10k 19 # bash-5.0 127048.200 usec/eval: arr2=("${arr1[@]}") (x1) 20 # bash-4.4 14800.000 usec/eval: arr2=("${arr1[@]}") (x10) 21 # bash-4.3 17900.000 usec/eval: arr2=("${arr1[@]}") (x10) 22 # bash-4.2 104000.000 usec/eval: arr2=("${arr1[@]}") (x1) 23 # bash-4.1 106000.000 usec/eval: arr2=("${arr1[@]}") (x1) 24 # bash-4.0 23500.000 usec/eval: arr2=("${arr1[@]}") (x10) 25 # bash-3.2 61000.000 usec/eval: arr2=("${arr1[@]}") (x2) 26 # bash-3.1 104000.000 usec/eval: arr2=("${arr1[@]}") (x1) 27 28 # 100k 29 # bash-5.0 1384414.200 usec/eval: arr2=("${arr1[@]}") (x1) 30 # bash-4.4 116000.000 usec/eval: arr2=("${arr1[@]}") (x1) 31 # bash-4.3 1134000.000 usec/eval: arr2=("${arr1[@]}") (x1) 32 # bash-4.2 1155000.000 usec/eval: arr2=("${arr1[@]}") (x1) 33 # bash-4.1 1168000.000 usec/eval: arr2=("${arr1[@]}") (x1) 34 # bash-4.0 1183000.000 usec/eval: arr2=("${arr1[@]}") (x1) 35 # bash-3.2 1408000.000 usec/eval: arr2=("${arr1[@]}") (x1) 36 # bash-3.1 1230000.000 usec/eval: arr2=("${arr1[@]}") (x1) 37 38 39 function ble/array#set { eval "$1=(\"\${@:2}\")"; } 40 #ble-measure 'ble/array#set arr2 "${arr1[@]}"' 41 42 # Bash-5.0 以外では一旦関数の引数にした方が速い 43 44 # bash-5.0 14088.700 usec/eval: ble/array#set arr2 "${arr1[@]}" (x10) 45 # bash-4.4 52000.000 usec/eval: ble/array#set arr2 "${arr1[@]}" (x2) 46 # bash-4.3 24400.000 usec/eval: ble/array#set arr2 "${arr1[@]}" (x5) 47 # bash-4.2 26400.000 usec/eval: ble/array#set arr2 "${arr1[@]}" (x5) 48 # bash-4.1 35400.000 usec/eval: ble/array#set arr2 "${arr1[@]}" (x5) 49 # bash-4.0 36600.000 usec/eval: ble/array#set arr2 "${arr1[@]}" (x5) 50 # bash-3.2 35200.000 usec/eval: ble/array#set arr2 "${arr1[@]}" (x5) 51 # bash-3.1 35800.000 usec/eval: ble/array#set arr2 "${arr1[@]}" (x5) 52 53 54 #------------------------------------------------------------------------------ 55 # 配列追記 56 57 function ble/array#push.1 { eval "$1+=(\"\${@:2}\")"; } 58 function ble/array#push.2 { eval "$1=(\"\${$1[@]}\" \"\${@:2}\")"; } 59 60 #ble-measure 'arr2=({0..10000})' 61 # bash-5.0 4759.662 usec/eval: arr2=({0..10000}) (x50) 62 # bash-4.4 4678.000 usec/eval: arr2=({0..10000}) (x50) 63 # bash-4.3 4438.000 usec/eval: arr2=({0..10000}) (x50) 64 # bash-4.2 5316.000 usec/eval: arr2=({0..10000}) (x50) 65 # bash-4.1 6495.000 usec/eval: arr2=({0..10000}) (x20) 66 # bash-4.0 6595.000 usec/eval: arr2=({0..10000}) (x20) 67 # bash-3.2 6495.000 usec/eval: arr2=({0..10000}) (x20) 68 # bash-3.1 6645.000 usec/eval: arr2=({0..10000}) (x20) 69 70 #ble-measure 'arr2=({0..10000}); ble/array#push.1 arr2 {1..1000}' 71 # bash-5.0 6201.960 usec/eval: arr2=({0..10000}); ble/array#push.1 arr2 {1..1000} (x20) 72 # bash-4.4 5795.000 usec/eval: arr2=({0..10000}); ble/array#push.1 arr2 {1..1000} (x20) 73 # bash-4.3 11600.000 usec/eval: arr2=({0..10000}); ble/array#push.1 arr2 {1..1000} (x10) 74 # bash-4.2 10600.000 usec/eval: arr2=({0..10000}); ble/array#push.1 arr2 {1..1000} (x10) 75 # bash-4.1 13100.000 usec/eval: arr2=({0..10000}); ble/array#push.1 arr2 {1..1000} (x10) 76 # bash-4.0 70500.000 usec/eval: arr2=({0..10000}); ble/array#push.1 arr2 {1..1000} (x2) 77 # bash-3.2 69000.000 usec/eval: arr2=({0..10000}); ble/array#push.1 arr2 {1..1000} (x2) 78 # bash-3.1 68500.000 usec/eval: arr2=({0..10000}); ble/array#push.1 arr2 {1..1000} (x2) 79 80 #ble-measure 'arr2=({0..10000}); ble/array#push.2 arr2 {1..1000}' 81 # bash-5.0 15342.200 usec/eval: arr2=({0..10000}); ble/array#push.2 arr2 {1..1000} (x10) 82 # bash-4.4 24000.000 usec/eval: arr2=({0..10000}); ble/array#push.2 arr2 {1..1000} (x5) 83 # bash-4.3 15700.000 usec/eval: arr2=({0..10000}); ble/array#push.2 arr2 {1..1000} (x10) 84 # bash-4.2 106000.000 usec/eval: arr2=({0..10000}); ble/array#push.2 arr2 {1..1000} (x1) 85 # bash-4.1 119000.000 usec/eval: arr2=({0..10000}); ble/array#push.2 arr2 {1..1000} (x1) 86 # bash-4.0 119000.000 usec/eval: arr2=({0..10000}); ble/array#push.2 arr2 {1..1000} (x1) 87 # bash-3.2 116000.000 usec/eval: arr2=({0..10000}); ble/array#push.2 arr2 {1..1000} (x1) 88 # bash-3.1 113000.000 usec/eval: arr2=({0..10000}); ble/array#push.2 arr2 {1..1000} (x1) 89 90 #ble-measure 'arr2=({0..1000}); ble/array#push.1 arr2 "${arr1[@]}"' 91 # bash-5.0 14919.310 usec/eval: arr2=({0..1000}); ble/array#push.1 arr2 "${arr1[@]}" (x10) 92 # bash-4.4 78000.000 usec/eval: arr2=({0..1000}); ble/array#push.1 arr2 "${arr1[@]}" (x2) 93 # bash-4.3 25800.000 usec/eval: arr2=({0..1000}); ble/array#push.1 arr2 "${arr1[@]}" (x5) 94 # bash-4.2 28000.000 usec/eval: arr2=({0..1000}); ble/array#push.1 arr2 "${arr1[@]}" (x5) 95 # bash-4.1 36400.000 usec/eval: arr2=({0..1000}); ble/array#push.1 arr2 "${arr1[@]}" (x5) 96 # bash-4.0 372000.000 usec/eval: arr2=({0..1000}); ble/array#push.1 arr2 "${arr1[@]}" (x1) 97 # bash-3.2 273000.000 usec/eval: arr2=({0..1000}); ble/array#push.1 arr2 "${arr1[@]}" (x1) 98 # bash-3.1 267000.000 usec/eval: arr2=({0..1000}); ble/array#push.1 arr2 "${arr1[@]}" (x1) 99 100 #ble-measure 'arr2=({0..1000}); ble/array#push.2 arr2 "${arr1[@]}"' 101 # bash-5.0 15667.830 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 "${arr1[@]}" (x10) 102 # bash-4.4 101000.000 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 "${arr1[@]}" (x1) 103 # bash-4.3 25600.000 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 "${arr1[@]}" (x5) 104 # bash-4.2 28000.000 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 "${arr1[@]}" (x5) 105 # bash-4.1 37600.000 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 "${arr1[@]}" (x5) 106 # bash-4.0 37600.000 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 "${arr1[@]}" (x5) 107 # bash-3.2 37200.000 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 "${arr1[@]}" (x5) 108 # bash-3.1 37400.000 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 "${arr1[@]}" (x5) 109 110 #ble-measure 'arr2=({0..1000}); ble/array#push.2 arr2 {0..10000}' 111 # bash-5.0 133900.200 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 {0..10000} (x1) 112 # bash-4.4 106000.000 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 {0..10000} (x1) 113 # bash-4.3 21800.000 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 {0..10000} (x5) 114 # bash-4.2 22600.000 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 {0..10000} (x5) 115 # bash-4.1 29400.000 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 {0..10000} (x5) 116 # bash-4.0 30400.000 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 {0..10000} (x5) 117 # bash-3.2 29800.000 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 {0..10000} (x5) 118 # bash-3.1 29200.000 usec/eval: arr2=({0..1000}); ble/array#push.2 arr2 {0..10000} (x5) 119 120 #------------------------------------------------------------------------------