Python is one among the foremost wide used programming languages and has been around for over 28 years. one among the foremost common queries within the minds of most of the people, particularly novices and beginners, is why Python is fashionable within the thought despite its slowness? Or why don’t programmers or developers care concerning speed and performance limitations in Python? during this publication, i will be able to address a number of the most reasons for this.
- High-level programming language
- Python is interpreted and not compiled
- Python is a dynamically written language
- Global Translator Lock (GIL)
But Python Still Popular
Why Python is Still Popular ?
- Active Community
- easy to analyze and organize the data
- offer cross-platform support
- Python is very easy to learn and use
prerequisite to Learn Python Programming ?
Fundamentals of Computers and programming Enough to Start the Python Courses to start your Career.
I will answer this by first answering, why Python is slower than Java. I hope this will make sense in the end.
First and foremost, the interpreted vs. compiled distinction that some other answers mention is actually irrelevant. Indeed, surprisingly enough to most, Python is a compiled language running on a virtual machine, just like Java. That’s exactly what those .PYC files you sometimes see are — Python bytecode.
So why is Python so vastly slower than Java, even though they effectively follow the same principles of execution?
Mostly, it comes down to the fact that Python is a dynamic language, which means that a lot of things normally taken care of during compilation, have to be moved into runtime instead. For example, dynamic method lookup can be very expensive, and Python compiler has absolutely no means to convert any of these calls into static method bindings — something that is trivial in the case of Java. This also has a knock-on effect during execution as well — since the execution path is much more predictable in Java, when the just-in-time native code compiler kicks in, there are a lot of optimizations that can be performed, quite on par with the optimizations a decent C++ compiler would make.
I hope this gives you some insight into the underlying issues, even though you asked for comparison with C. Python offers a lot of useful abstractions, such as not caring about method parameter type until it is actually used, but each abstraction comes at a price, and in the case of Python, that price is paid primarily during runtime. This is very much in contrast to something like C, C++, and Java, where almost all of the abstraction price is covered during the compilation. Add the fact that C is very close to machine code itself (the only thing closer is Assembly), which allows a programmer to fine-tune the code to incredible detail, and you have pretty much the full picture.
Let me end this with an example of very simple operation, and how it is essentially performed by C and Python:
- c = a + b
- In C:
- – load the integer from location a
- – load the integer from location b
- – perform integer addition
- – store the resulting integer into location c
- Total: 4 CPU commands
- In Python:
- – load the object from location a
- – load the object from location b
- – find the __add__ method in object a
- – … hashtable magic here …
- – perform type checks and do the necessary type casting for object b
- – if you can‘t find an appropriate representation for b, throw an exception
- – extract the integer value from object a
- – extract the integer value from object b
- – perform integer addition
- – allocate new memory
- – wrap the resulting value in a new object
- Total: a few hundred CPU commands.
I want to answer this question: When Python completes a comparable application 2–10x slower than another language, why is it slow and can’t we make it faster?
Here are the top theories:
- “It’s the GIL (Global Interpreter Lock)”
- “It’s because its interpreted and not compiled”
- “It’s because its a dynamically typed language”
Which one of these reasons has the biggest impact on performance?