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.

  1. High-level programming language
  2. Python is interpreted and not compiled
  3. Python is a dynamically written language
  4. 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:

  1. c = a + b
  2.  
  3. In C:
  4. load the integer from location a
  5. load the integer from location b
  6. perform integer addition
  7. store the resulting integer into location c
  8. Total: 4 CPU commands
  9.  
  10. In Python:
  11. load the object from location a
  12. load the object from location b
  13. find the __add__ method in object a
  14. hashtable magic here
  15. perform type checks and do the necessary type casting for object b
  16. if you can‘t find an appropriate representation for b, throw an exception
  17. – extract the integer value from object a
  18. – extract the integer value from object b
  19. – perform integer addition
  20. – allocate new memory
  21. – wrap the resulting value in a new object
  22. 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?

LEAVE A REPLY

Please enter your comment!
Please enter your name here