OILS / benchmarks / ysh-for.sh View on Github | oilshell.org

110 lines, 49 significant
1#!/usr/bin/env bash
2#
3# Benchmarks for YSH for loop
4#
5# Usage:
6# benchmarks/ysh-for.sh <function name>
7
8set -o nounset
9set -o pipefail
10set -o errexit
11
12YSH=_bin/cxx-opt/ysh
13OSH=_bin/cxx-opt/osh
14
15sum() {
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
28sum-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
42sum-py() {
43 echo ' PY'
44 time python3 -c '
45import sys
46n = int(sys.argv[1])
47sum = 0
48for i in range(n):
49 sum += i
50print(f"sum = {sum}")
51 ' "$@"
52}
53
54sum-sh() {
55 local sh=$1
56 local n=$2
57
58 echo " $sh"
59 time $sh -c '
60n=$1
61sum=0
62i=0
63while test $i -lt $n; do
64 sum=$(( sum + i ))
65 i=$(( i + 1 ))
66done
67echo "sum = $sum"
68 ' "$@"
69}
70
71compare() {
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"$@"