Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python vs Bash - In which kind of tasks each one outruns the other performance-wise? [closed]

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?

like image 568
Doppelganger Avatar asked Mar 11 '10 12:03

Doppelganger


People also ask

Which is faster bash or Python?

Python is faster than Bash and is ranked 1st, while Bash is ranked 34th. The most important reasons people chose Python are that it can be used for almost any task. It works on most major operating systems and is also installed by default on most Unix/Linus systems. It is very similar to writing pseudocode.

Which is better bash or Python?

Bash is a software replacement for the original Bourne shell. Python is easy, simple and powerful language. Bash is tough to write and not powerful as python. It is specially designed for web and app development.

Is bash the same as Python?

No. Python is a programming language mostly used in automation programming. Bash is a command-line interpreter or user shell to interpret user commands. Python is developed as an easy to implement an object-oriented programming language.

Why is bash so slow?

Bash scripts will always be slower than compiled code as they need to be interpreted. As you probably know, in order to run your code written in C you first need to compile it. When it comes to Bash scripts, you don't have to read it, the code is just "read on the fly". So Bash is slower than C.


2 Answers

Typical mainframe flow...

Input Disk/Tape/User (runtime) --> Job Control Language (JCL) --> Output Disk/Tape/Screen/Printer                                    |                          ^                                    v                          |                                    `--> COBOL Program --------'  

Typical Linux flow...

Input Disk/SSD/User (runtime) --> sh/bash/ksh/zsh/... ----------> Output Disk/SSD/Screen/Printer                                    |                          ^                                    v                          |                                    `--> Python script --------'                                    |                          ^                                    v                          |                                    `--> awk script -----------'                                    |                          ^                                    v                          |                                    `--> sed script -----------'                                    |                          ^                                    v                          |                                    `--> C/C++ program --------'                                    |                          ^                                    v                          |                                    `--- Java program ---------'                                    |                          ^                                    v                          |                                    :                          : 

Shells are the glue of Linux

Linux shells like sh/ksh/bash/... provide input/output/flow-control designation facilities much like the old mainframe Job Control Language... but on steroids! They are Turing complete languages in their own right while being optimized to efficiently pass data and control to and from other executing processes written in any language the O/S supports.

Most Linux applications, regardless what language the bulk of the program is written in, depend on shell scripts and Bash has become the most common. Clicking an icon on the desktop usually runs a short Bash script. That script, either directly or indirectly, knows where all the files needed are and sets variables and command line parameters, finally calling the program. That's a shell's simplest use.

Linux as we know it however would hardly be Linux without the thousands of shell scripts that startup the system, respond to events, control execution priorities and compile, configure and run programs. Many of these are quite large and complex.

Shells provide an infrastructure that lets us use pre-built components that are linked together at run time rather than compile time. Those components are free-standing programs in their own right that can be used alone or in other combinations without recompiling. The syntax for calling them is indistinguishable from that of a Bash builtin command, and there are in fact numerous builtin commands for which there is also a stand-alone executable on the system, often having additional options.

There is no language-wide difference between Python and Bash in performance. It entirely depends on how each is coded and which external tools are called.

Any of the well known tools like awk, sed, grep, bc, dc, tr, etc. will leave doing those operations in either language in the dust. Bash then is preferred for anything without a graphical user interface since it is easier and more efficient to call and pass data back from a tool like those with Bash than Python.

Performance

It depends on which programs the Bash shell script calls and their suitability for the subtask they are given whether the overall throughput and/or responsiveness will be better or worse than the equivalent Python. To complicate matters Python, like most languages, can also call other executables, though it is more cumbersome and thus not as often used.

User Interface

One area where Python is the clear winner is user interface. That makes it an excellent language for building local or client-server applications as it natively supports GTK graphics and is far more intuitive than Bash.

Bash only understands text. Other tools must be called for a GUI and data passed back from them. A Python script is one option. Faster but less flexible options are the binaries like YAD, Zenity, and GTKDialog.

While shells like Bash work well with GUIs like Yad, GtkDialog (embedded XML-like interface to GTK+ functions), dialog, and xmessage, Python is much more capable and so better for complex GUI windows.

Summary

Building with shell scripts is like assembling a computer with off-the-shelf components the way desktop PCs are.

Building with Python, C++ or most any other language is more like building a computer by soldering the chips (libraries) and other electronic parts together the way smartphones are.

The best results are usually obtained by using a combination of languages where each can do what they do best. One developer calls this "polyglot programming".

like image 195
DocSalvager Avatar answered Sep 24 '22 19:09

DocSalvager


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.

like image 43
Kevin Little Avatar answered Sep 23 '22 19:09

Kevin Little