views:

310

answers:

4

Obviously Python is more user friendly, a quick search on google shows many results that say that, as Python is byte-compiled is usually faster. I even found this that claims that you can see an improvement of over 2000% on dictionary-based operations.

What is your experience on this matter? In which kind of task each one is a clear winner?

A: 

There are 2 scenario's where Bash performance is at least equal I believe:

  • Scripting of command line utilities
  • Scripts which take only a short time to execute; where starting the Python interpreter takes more time than the operation itself

That said, I usually don't really concern myself with performance of the scripting language itself. If performance is a real issue you don't script but program (possibly in Python).

extraneon
+2  A: 

Developer efficiency matters much more to me in scenarios where both bash and Python are sensible choices.

Some tasks lend themselves well to bash, and others to Python. It also isn't unusual for me to start something as a bash script and change it to Python as it evolves over several weeks.

A big advantage Python has is in corner cases around filename handling, while it has glob, shutil, subprocess, and others for common scripting needs.

Roger Pate
A: 

Bash is primarily a batch / shell scripting language with far less support for various data types and all sorts of quirks around control structures -- not to mention compatibility issues.

Which is faster? Neither, because you are not comparing apples to apples here. If you had to sort an ascii text file and you were using tools like zcat, sort, uniq, and sed then you will smoke Python performance wise.

However, if you need a proper programming environment that supports floating point and various control flow, then Python wins hands down. If you wrote say a recursive algorithm in Bash and Python, the Python version will win in an order of magnitude or more.

Justin
So the whole moral of my rant is: use the right tool for the right job.
Justin
floating point is supported with tools like awk, bc and with shells like zsh/ksh, so why do you say Python wins hands down?
ghostdog74
Because those tools are not Bash. I was pointing out a distinct difference. Those tools are used in a shell script, but native Bash itself does not support floating point.
Justin
How can you say that using external unix tools in a shell script would be faster than using native libraries? Doesn't each call to an external unix command requires a new fork/exec of the shell?
frankc
No. Try it yourself. gzip a large log file and use zcat, sort, etc to do some filtering and then use the native Python libs. It's significantly faster using the native tools.
Justin
@justin, yes, these tools are not Bash but they have been around since ancient times and are often used in shell scripting. if you want floating point, use awk/bc. Its a combination of these tools that make shell scripting just as powerful as Python.
ghostdog74
+3  A: 

Generally, bash works better than python only in those environments where python is not available. :)

Seriously, I have to deal with both languages daily, and will take python instantly over bash if given the choice. Alas, I am forced to use bash on certain "small" platforms because someone has (mistakenly, IMHO) decided that python is "too large" to fit.

While it is true that bash might be faster than python for some select tasks, it can never be as quick to develop with, or as easy to maintain (at least after you get past 10 lines of code or so). Bash's sole strong point wrt python or ruby or lua, etc., is its ubiquity.

Kevin Little