Why C and C++ runs faster than Python, Perl and Ruby

This answer has the potential to get overly complex, so I'll try and keep it simple and concentrate on the “big picture”.
It basically boils down. To the difference between a statically typed (like C/C++) vs dynamically typed languages (like Python, Perl and Ruby).
In a static language like C/C++ the software being developed uses a closed system of types. The compiler has complete knowledge of the types it is required to support and can therefore perform extensive optimizations without any risk of the code not working.
As a quick example suppose we are adding 2 unsigned 32 bit ints. The c compiler can inline this into a couple of machine instructions, totally avoiding the need to generate a jump instruction and also eliminating the need to protect the register state on the stack.
Python can't do that kind of optimization. Since a function in Python is free to return literally any type imaginable or nothing at all
The return type of my two functions are unknowable when the byte codes are compiled. So Python and any similarly dynamic language needs to call both functions before it can evaluate which of multiple possible +operators is most appropriate for the types involved. When it finally resolves the address of the + operators, it makes a function call incurring the typical stack overhead.
In a dynamically typed language like Python the compiled bytecodes are run inside the python interpretter. The “compiling” is more a translation from text to binary this reduces the amout of i/o and eliminates the repeated parsing of Python sources so the code can load faster.
Now suppose I take my wonderful C++ function that adds 2 ints together and package as a library and distribute it. Then someone tries to use the library but they need to add 64 bit doubles. How does my code handle that? It doesn't it, doesn't have to. The errant user will immediately get a compiler error because the c++ compiler is very pedantic about types matching.
But lets say the errant user is very determined and they decided to edit the prototype in the header file I provided thinking they've found the loophole what happens next is they are able to compile but they get a linker error so they are still thwarted.
Why did I mention all that stuff about compilers, linkers and libraries? To point out that c++ (and static languages in general) forgo some convenience for the end user. Instead of making it work the way the user needed we throw up roadblocks to prevent incorrect usage.
But by limiting the scope of what the code is required to handle amazing performance gains are possible. Essentially with C++ you get the advantages of high level languages but with the speed of “bare metal” machine coding without the portability issues.
Depending on the application and the programmers attitude this tradeoff of convenience vs performance may or may not make sense. Also there really is such a thing as throwaway code, and in that case getting something done quickly in Python is a clear win over trying to do the same thing in C++.
It really boils down to knowing your tools, and selecting the right tool for the right job.
There are also ways to improve Python performance but these typically involve goving up some flexibility. For more in this google Cython.

Amazing Facts about Programming

Amazing Facts about programming  The first programmer was a lady named Ada Lovelac . She was a writer and gifted mathematician...

Popular Tutorials