Skip navigation.

Raphael's Blog

A look into a programmer's life

"stress"-testing shell interpreters...

Bash, dash, posh, ksh, zsh, ... which one is the fastest one summing numbers?

So here's the test script:
s=$(date +%s); c=0; while true; do if [ $c = 1000000 ]; then break; else c=$(($c+1)); fi; done; echo "$0 took" $(($(date +%s) - $s)) "seconds to finish"


And the results:

$ bash -c 's=$(date +%s); c=0; while true; do if [ $c = 1000000 ]; then break; else c=$(($c+1)); fi; done; echo "$0 took" $(($(date +%s) - $s)) "seconds to finish"'
bash took 53 seconds to finish


$ dash -c 's=$(date +%s); c=0; while true; do if [ $c = 1000000 ]; then break; else c=$(($c+1)); fi; done; echo "$0 took" $(($(date +%s) - $s)) "seconds to finish"'
dash took 8 seconds to finish


$ zsh -c 's=$(date +%s); c=0; while true; do if [ $c = 1000000 ]; then break; else c=$(($c+1)); fi; done; echo "$0 took" $(($(date +%s) - $sk)) "seconds to finish"'
zsh took 39 seconds to finish


$ ksh -c 's=$(date +%s); c=0; while true; do if [ $c = 1000000 ]; then break; else c=$(($c+1)); fi; done; echo "$0 took" $(($(date +%s) - $s)) "seconds to finish"'
ksh took 14 seconds to finish



Intel P4 2.80 GHz, 1MB cache
bash: 3.1dfsg-8
dash: 0.5.4-6
ksh: 93s+20070628-1
posh: 0.6
zsh: 4.3.4-dev-7-3

... and posh's results? I killed the process after seven minutes :worried: (smells like a bug)

a simple bashism-powered quinewhere to put such a script?...

Comments

Anonymous 11. February 2008, 02:03

Matheusgr writes:

Did you try to use Bash with C locale?

LANG=en_US.UTF-8 bash -c 's=$(date +%s); c=0; while true; do if [ $c = 1000000 ]; then break; else c=$(($c+1)); fi; done; echo "$0 took" $(($(date +%s) - $s)) "seconds to finish"'
bash took 43 seconds to finish

LANG=C bash -c 's=$(date +%s); c=0; while true; do if [ $c = 1000000 ]; then break; else c=$(($c+1)); fi; done; echo"$0 took" $(($(date +%s) - $s)) "seconds to finish"'
bash took 32 seconds to finish

I dont know if any other shell will also have a different execution time.

atomo64 11. February 2008, 03:37

Didn't do it with LANG=C

$ LANG=C bash -c 's=$(date +%s); c=0; while true; do if [ $c = 1000000 ]; then break; else c=$(($c+1)); fi; done; echo "$0 took" $(($(date +%s) - $s)) "seconds to finish"'
bash took 37 seconds to finish



$ LANG=C zsh -c 's=$(date +%s); c=0; while true; do if [ $c = 1000000 ]; then break; else c=$(($c+1)); fi; done; echo "$0 took" $(($(date +%s) - $s)) "seconds to finish"'
zsh took 33 seconds to finish



the others don't differ at all with LANG=C

Anonymous 11. February 2008, 14:52

Anonymous writes:

There's a type in the zsh script. Instead of $sk it has to be $s.

Anyway, with zsh-4.3.5 it takes about 23 seconds on my Centrino Duo with 1.6 GHz and 1G RAM. Using LANG=C seems to have no effect. My default is en_US.utf8.

--Tassilo

Anonymous 11. February 2008, 18:16

Alan Pope writes:

This reminds me of the perl "benchmark" (Note: not a benchmark) that we've been running on loads of machines. It's much like yours, and the results are available for all to see:-

http://www.hants.lug.org.uk/cgi-bin/wiki.pl?HugoRandomBenchmark

atomo64 12. February 2008, 02:01

There's a type in the zsh script. Instead of $sk it has to be $s.


seems like I accidentally inserted a 'k' while separating the test results (backed up with the fact that trying to run it that way you would get a zsh:1: bad math expression: operand expected at `')

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies