1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Benchmarks for YSH for loop
|
4 | #
|
5 | # Usage:
|
6 | # benchmarks/ysh-for.sh <function name>
|
7 |
|
8 | set -o nounset
|
9 | set -o pipefail
|
10 | set -o errexit
|
11 |
|
12 | YSH=_bin/cxx-opt/ysh
|
13 | OSH=_bin/cxx-opt/osh
|
14 |
|
15 | sum() {
|
16 | echo " YSH for loop"
|
17 |
|
18 | time $YSH -c '
|
19 | var sum = 0
|
20 | for i in (0 .. $1) {
|
21 | setvar sum += i
|
22 | }
|
23 | echo "i = $i"
|
24 | echo "sum = $sum"
|
25 | ' dummy "$@"
|
26 | }
|
27 |
|
28 | sum-closures() {
|
29 | echo " YSH closures"
|
30 |
|
31 | time $YSH -c '
|
32 | var sum = 0
|
33 | for __hack__ in (0 .. $1) { # trigger allocation
|
34 | setvar sum += __hack__
|
35 | }
|
36 | # Does not leak!
|
37 | #echo "__hack__ = $__hack__"
|
38 | echo "sum = $sum"
|
39 | ' dummy "$@"
|
40 | }
|
41 |
|
42 | sum-py() {
|
43 | echo ' PY'
|
44 | time python3 -c '
|
45 | import sys
|
46 | n = int(sys.argv[1])
|
47 | sum = 0
|
48 | for i in range(n):
|
49 | sum += i
|
50 | print(f"sum = {sum}")
|
51 | ' "$@"
|
52 | }
|
53 |
|
54 | sum-sh() {
|
55 | local sh=$1
|
56 | local n=$2
|
57 |
|
58 | echo " $sh"
|
59 | time $sh -c '
|
60 | n=$1
|
61 | sum=0
|
62 | i=0
|
63 | while test $i -lt $n; do
|
64 | sum=$(( sum + i ))
|
65 | i=$(( i + 1 ))
|
66 | done
|
67 | echo "sum = $sum"
|
68 | ' "$@"
|
69 | }
|
70 |
|
71 | compare() {
|
72 | local n=${1:-1000000}
|
73 | local OILS_GC_STATS=${2:-}
|
74 |
|
75 | ninja $OSH $YSH
|
76 |
|
77 | sum-py $n
|
78 | echo
|
79 |
|
80 | export OILS_GC_STATS
|
81 | sum $n
|
82 | echo
|
83 |
|
84 | sum-closures $n
|
85 | echo
|
86 |
|
87 | if true; then
|
88 | # 3.9 seconds
|
89 | sum-sh bash $n
|
90 | echo
|
91 |
|
92 | # 3.7 seconds
|
93 | sum-sh $OSH $n
|
94 | echo
|
95 |
|
96 | # 1.2 seconds
|
97 | sum-sh dash $n
|
98 | echo
|
99 |
|
100 | # 2.3 seconds
|
101 | sum-sh zsh $n
|
102 | echo
|
103 |
|
104 | # 3.1 seconds
|
105 | sum-sh mksh $n
|
106 | echo
|
107 | fi
|
108 | }
|
109 |
|
110 | "$@"
|