I was testing the speed of Bash and Python by running a loop 1 billion times.
$ cat python.py
#!/bin/python
# python v3.5
i=0;
while i<=1000000000:
i=i+1;
Bash code:
$ cat bash2.sh
#!/bin/bash
# bash v4.3
i=0
while [[ $i -le 1000000000 ]]
do
let i++
done
Using the time
command I found out that the Python code takes just 48 seconds to finish while the Bash code took over 1 hour before I killed the script.
Why is this so? I expected that Bash would be faster. Is there something wrong with my script or is Bash really much slower with this script?
Best Answer
This is a known bug in bash; see the man page and search for "BUGS":
;)
For an excellent primer on the conceptual differences between shell scripting and other programming languages, I highly recommend reading:
The most pertinent excerpts:
Don't use big loops in shell scripting.