Is Python slow? Use Cases and comparison with its peers :)
Python is well known to be one of the most useful programming languages. You can do almost anything with it, from web applications, through image processing, to data science and machine learning. It’s like a Swiss army knife for programmers. However, some people still claim several superstitions.
Although Python is easy to learn because of its syntax and the fact that it is a dynamic programming language, some mention that it might be too slow. At IT Sharkz we had similar doubts, but our experience since then has proven that these concerns were unfounded. So we will try to tell you the truth as objectively as possible.
What does it mean that a language is slow?
Each programming language can be described in different ways. In terms of language efficiency, two categories come to mind: speed of production and performance. In which of these categories could we say that Python is slow? Let’s check!
Production / delivery speed
In many cases, the speed of production and delivery of a language is its most important parameter. It tells us when we can potentially expect delivery of a finished application and allows us to design an appropriate release schedule. Since Python can be used in almost all stages of a project (since we can use it to create the application, test it and prepare the project environment), a single skilled developer could handle the whole production process from start to finish.
Due to the higher development speed, the use of Python could indeed result in a smaller number of developers required for a project, which would have the added benefit of reducing project costs.
But taking care of a project is not only about creating brand new applications, it also means continuous maintenance and support. The code written in Python is very clear and easy to read thanks to its syntax and the PEP8 standard.
Graph showing the average number of hours needed to solve a problem (Prechelt and Garret)
As you can see in the graph above, there is no doubt that Python performs very well compared to other languages in terms of production speed.
This parameter tells us how fast a programming language can handle a problem. At the moment, the languages that shine the most in this respect are C and C++. Indeed, it makes sense to use them in certain cases, such as creating video games, compilers, or whenever a large number of calculations and good memory control are required. But what about other cases?
Each of the above use cases requires a huge amount of computation. Nevertheless, Python continues to be the most popular language used in machine learning or image processing. Let me now explain why.
In the first paragraph I mentioned that Python is often accused of being slow, which is only partially true. I don’t want to accuse anyone, but the truth is that most of the time this argument is used by beginners.
If your project uses machine learning, more often than not, it will have to rely on some external libraries, like SciKit and TensorFlow, both of which are available for Python.
Since these libraries are written in C++, when used in Python scripts, we use C++ in the background. This makes the development process fast and makes our script as fast as those written in C++. The same goes for image preprocessing (OpenCV library). You can clearly see that in this case too, Python serves as a wrapper for C++.
Python is great for building web applications. And when you start your web development journey, it’s not exactly easy to choose your first web framework and when it comes to Python, the selection on offer is quite large: we have Django, Flask, TurboGears and many more.
Django is by far the most popular Python web framework, and it powers some of the biggest apps and websites you know, including Spotify, and YouTube (though it was originally built in PHP), and BitBucket.
If Python was as bad as some people claim, these projects probably wouldn’t have been as successful as they were. Also, assuming that the performance of a web application depends solely on the code and language chosen is a huge mistake.
The performance of an application depends on a number of factors, and properly written and optimized code is only one of them. Overall performance is also limited by bottlenecks. What are bottlenecks? Well, the name is pretty self-explanatory. Fluid speed is limited by the bottlenecks it encounters. What differentiates a good developer from a bad developer is that the good ones know when to optimize the code. Nick Humrich, Python guru and former Amazon engineer, explains this sentiment very well:
If your optimizations aren’t touching the bottleneck, you’re wasting your time and not fixing the real issue. You won’t get any serious improvements until you optimize the bottleneck (…) Optimizing code before you measure and determine where the bottleneck is, is known as “premature optimization”. – Nick Humrich
If you still think that optimization is the most important thing to increase application speed, maybe someone like Donald Knuth, the author of The Art of Computer Programming, will convince you:
We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%. – Donald Knuth
Spending countless hours improving code just to make it a few nanoseconds faster makes no sense, especially from the customer’s point of view.
Is Python slow? While native Python code may seem to have a high execution time, especially when considering benchmarks, when it comes to commercial use, the speed it offers is more than satisfactory for most applications.
What makes Python so powerful? Mainly its high level development speed and variety of uses. Furthermore, it would be hard to find a programming language that makes application development easier and more enjoyable than Python.