OILS / benchmarks / compute / array_ref.sh View on Github | oilshell.org

46 lines, 23 significant
1#!/usr/bin/env bash
2#
3# Show superlinear behavior in bash arrays. Need pretty high N to start seeing
4# it.
5#
6# Usage:
7# ./array_ref.sh MODE
8
9set -o nounset
10set -o pipefail
11set -o errexit
12
13main() {
14 local mode=$1
15
16 mapfile -t array
17
18 local n=${#array[@]}
19 local sum=0
20
21 case $mode in
22 linear)
23 for (( i = 0; i < n; ++i )); do
24 sum=$((sum + array[i]))
25 done
26 ;;
27
28 random)
29 for (( i = 0; i < n; ++i )); do
30 # Super linear
31 sum=$((sum + array[array[i]]))
32 done
33 ;;
34 esac
35
36 echo sum=$sum
37
38 # This doesn't seem to defeat LASTREF?
39 #array+=('X')
40 #unset 'array[-1]'
41
42 # neither does this
43 #array[i]=$i
44}
45
46main "$@"