This website uses cookies for analytics and to improve provided services. By choosing I Accept, you consent to our use of them and other tracking technologies according to our Privacy Policy
Slow

Is Python Slow?

Python is well known to be one of the most useful programming languages. You can do almost anything with it—from Web/desktop applications, through image processing, up to data science and machine learning. It’s like a Swiss army knife for programmers. However, some developers continue to claim that although Python is easy to learn, it is simply too slow. I have been programming in Python for over five years now and in the early days of my time with the language, I had similar doubts, but my experience since has proven these concerns to be unfounded. So, I will try to lay out the truth for you as objectively as I possibly can.

What does it mean that a language is slow?

Every programming language can be described in a variety of ways. In terms of language effectiveness, two categories come to mind—code delivery speed and performance. In which of these is Python considered slow? Let’s check!

Delivery speed

In many cases, the delivery speed of a language is its most important parameter. It tells us when we can potentially expect delivery of a finished app and allows us to design proper release timing. Because Python can be used at almost every stage of a given project (we can use it to create the application, test it, and prepare the project environment), one skilled developer could take care of the whole delivery process from start to finish.

Because of the higher development speed, using Python might actually translate into a smaller number of developers required for a project, bringing the additional benefit of reducing project costs.

But dealing with projects is more than just creating brand new applications—it also means continued maintenance and support. Code written in Python is very crisp and easy to read thanks to its syntax and PEP8 standard. Just take a look at this sample quicksort implementation written in different languages:

JAVA

 
import java.util.*;

import java.util.function.*;

import java.util.stream.Stream;

 

import static com.google.common.collect.Iterables.concat;

import static java.lang.System.out;

import static java.util.Arrays.asList;

import static java.util.stream.Collectors.partitioningBy;

import static org.assertj.core.util.Lists.newArrayList;

 

public  List qs(List l, BiPredicate<t, t=""> isLower) {

if (l.size() < 2) {

return l;

} else {

T x = l.get(0);

Stream xs = l.stream().skip(1);

Map<boolean, list<t="">> part = xs.collect(partitioningBy(item -> isLower.test(item, x)));

List lowers = part.get(true);

List highers = part.get(false);

 

return newArrayList(concat(qs(lowers, isLower), asList(x), qs(highers, isLower)));

}

}

</boolean,></t,>

Python


def qsort(L):

if L == []: return []

return qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + \

qsort([x for x in L[1:] if x>=L[0]])

You can find the full list here

 

graph showing median hours to solve a problem

How long does it take to write a string processing application in various languages. (Prechelt and Garret

 

As you can see in the graph above, there can be no doubt that Python absolutely outperforms other languages in terms of delivery speed.

Performance

This parameter tells us how quickly a programming language can deal with a problem. As of this writing, the languages that shine brightest in that respect are C and C++. And indeed, it makes sense to use them in some cases, such as creating computer games, compilers, or whenever there’s a need for a large volume of calculations and good memory control. But what about other cases? What about Web development or—to name a few recent hot topics —data science, image preprocessing, and machine learning?

Python in data science, image preprocessing, and machine learning

Every single use case mentioned above requires a huge amount of calculations. Nevertheless, Python continues to be the most popular language used in machine learning or image processing. Now let me explain why.

 

illustration - Machine Learning
 
Source: https://www.geeksforgeeks.org/machine-learning/

 

Back in the opening paragraph, I mentioned that Python is often accused of being slow, which is only partially true. I don’t want to insult anyone, but the truth is that most of the time this argument is used by beginners.

If the project uses machine learning, more often than not, it will need to draw on some external libraries, such as SciKit and TensorFlow, both of which are available for Python.

Because those libraries are written in C++, when using them in Python scripts, we’re using C++ in the background. That makes the development process fast because of Python, and makes our script as fast as those written in C++. The same is true for image preprocessing (OpenCV library). You can clearly see that in this case, too, Python serves as a kind of a “wrapper” for C++.

Web development

Python is great for creating Web applications. And when starting 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 rather broad: we have Django, Flask, TurboGears and many others.

Django is by far the most popular Python Web framework, and it powers some of the biggest apps and websites you know, including Spotify, Washington Post, and YouTube (although it was initially built with PHP), and BitBucket.

Was Python so bad as some people claim, those projects probably wouldn’t have been as successful as they have. Furthermore, assuming that the performance of a Web application depends only on the code and chosen language is a huge mistake. The world is actually much more complicated than that.

Application performance is driven by a number of factors and properly written and optimized code is just one of them. Overall performance is also limited by bottlenecks. What are they? Well, the name is rather self-explanatory. Fluid velocity is limited by the bottlenecks it encounters. You are only as good as your weakest link, and it’s really rare for code to be the bottleneck. The thing that separates a good developer from a bad developer is that good ones know when to optimize code. Nick Humrich, Python Guru and former Amazon engineer, explains that sentiment quite 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 in order to boost the speed of the app, 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 the code just for the sake of making it a couple of nanoseconds faster doesn’t make sense, especially from the client’s perspective.

Conclusion

Is Python slow? Although native Python code may seem slow sometimes, especially when we look at the benchmarks, when it comes to commercial use, the speed it offers is more than satisfactory for most applications.

What makes Python so powerful? Primarily its top-shelf development speed and its variety of uses. Additionally, you’d be hard-pressed to find a programming language that makes developing applications easier and more pleasant than Python.

machine Why Use Python for Machine Learning?
python What Is Python and Why Is It so Popular?