1 | #!/bin/sh
|
2 | #
|
3 | # POSIX shell script to compute fibonacci many times in a loop. #
|
4 | # Shells have similar speeds:
|
5 | # dash: ~110 ms
|
6 | # ash: ~290 ms -- the fork is slower!
|
7 | # zsh: ~290 ms
|
8 | # mksh: ~380 ms
|
9 | # bash: ~430 ms
|
10 | # yash: ~460 ms
|
11 | #
|
12 | # Note: all shells use 64 bit integers on 64 bit platforms! But is that
|
13 | # justified? I want shell scripts to be portable!
|
14 | #
|
15 | # TODO: detect overflow in OSH.
|
16 | #
|
17 | # Note: fib(44) < 2^31, but fib(45) is greater
|
18 | # Note: fib(544) < 2^63, but fib(545) is greater
|
19 |
|
20 | iters=${1:-5} # first argument of every benchmark should be the number of iterations
|
21 |
|
22 | n=${2:-10} # fib(n)
|
23 |
|
24 | i=0
|
25 | while test $i -lt $iters; do
|
26 | j=0
|
27 |
|
28 | a=1 b=1
|
29 |
|
30 | while test $j -lt $n; do
|
31 | # a, b = b, a+b
|
32 | tmp=$b
|
33 | b=$((a+b))
|
34 | a=$tmp
|
35 |
|
36 | j=$((j+1))
|
37 | done
|
38 |
|
39 | echo $b
|
40 |
|
41 | i=$((i+1))
|
42 | done
|