If you wonder why Python is slower than other programming languages, you have come to the right place. In this article, let’s look at the reasons behind the slowness of the Python programming language. Let’s dive right in.
Is Python Really Slow?
Python is a high-level language compared to C++ and Java. A high-level language abstracts details of the computer for the programmer, which includes memory management, pointers, process, threads, etc. This abstraction simplifies and speeds up the coding process as it becomes closer to what humans think.
Python is closer to the human language, whereas Java and C++ are comparatively low-level languages closer to the hardware. The closer the code is to the hardware, the faster it will be.
As programmers say, C++/Java code runs many times faster than Python code. But if you consider the development time, Python almost beats most languages.
For some projects, the development time is more important than the run-time if you consider the cost of development. So basically, we cannot neglect Python due to its slow execution time.
Most of the time, you will not even notice the slowness of Python at execution time. When you build a project, the key thing is to develop the app as easy as possible. But for some projects, where the runtime should be faster, it’s better to use Java or C++ rather than Python.
The YouTube Story
Python beats many other languages despite being slower than them. For example, many years ago, Google was creating its own video platform using C++. At the same time, YouTube was a startup, and it was using Python.
Even though Google had more employees and resources than YouTube, it couldn’t compete with YouTube. YouTube could come up with more new features and updates because the development time was less while using Python.
Google Videos couldn’t keep up with that because they couldn’t implement features as fast as YouTube. Finally, Google had no other option but to acquire YouTube.
So you can’t say Python is slow and don’t use it. Yes, when it comes to the execution time of code, Python is slower than other languages like Java and C++.
Why Is Python Slow at Execution Time?
Now coming to the topic, let us understand why Python is slow at execution time.
1. Python Is an Interpreted Language
Python code execution is slow because code is interpreted at runtime instead of being compiled at compilation time. Since Python is a high-level and human-friendly language, Python code needs to go through many stages of abstraction before it can become executable machine code.
When you execute a Python code, the code gets compiled into Python byte code. Byte code is a lower-level code that gets executed faster. Python virtual machine will execute the byte code one by one.
In languages like C or C++, the code gets converted into executable binary code during compilation itself, and hence, it is more efficient.
2. Absence of Just In Time (JIT) Compiler
Other interpreted languages like Java and .NET run their respective Java bytecode and .NET bytecode faster than Python’s byte code. This is because the standard distribution of Java/.NET has a JIT compiler that converts bytecode to native code (machine language) at runtime.
Python lags these features and works differently. Just in Time compiler, as said above, converts the bytecode to computer understandable machine language and feeds in instructions to the CPU. Python cannot have a JIT compiler due to its dynamic structure.
A faster Python execution time is expected in future updates, but it is going to take a while before Python has an inbuilt JIT compiler.
3. Python Is a Dynamically Typed Language
One of the reasons for the slow speed of Python is that it is a dynamically typed language. Let us understand what dynamic typing and static typing are in programming.
In languages like C++/Java, variable datatype assignment takes place in the coding process where the coder explicitly mentions the datatype of the variable. This is the same for arrays too. This process is called static typing.
Now let’s come to Python. In Python, we do not mention the datatype in the code. Datatype assignment happens during execution time. This process is called dynamic typing.
The same goes for lists (arrays in Python) as well. Lists have an additional option of having different data types in the same list. This makes it even more time taking to perform during runtime.
So statically-typed languages are comparatively faster than dynamically-typed languages. However, the development time for dynamically-typed languages is less, and writing code is simplified.
4. Global Interpreter Lock (GIL)
In Python, if two threads (or two separate pieces of code) try to access one specific object in memory at the same time, we will run into issues. Python has a Global Interpreter Lock (GIL) to avoid this.
GIL is a type of process lock by Python while dealing with processes and threads. GIL prevents multi-threading by running only one thread within a single process at a time.
Python has a concept called a reference counter. It helps us to count the total number of references taken internally to assign a value to an object. When the count is zero, the object gets dereferenced.
Without a reference counter, a memory leak can take place when multi-threading happens (two threads try to write different values to the object). This can even cause deadlocks to happen.
So to prevent memory leaks and deadlocks, we use the Global Interpreter Lock. GIL provides thread-safe memory management.
This sounds like a wonderful feature, doesn’t it? Then how does this affect the speed at runtime? The global interpreter lock prevents CPU-bound tasks from executing in parallel. GIL doesn’t affect the performance of I/O-bound multi-threaded programs because the lock is shared between threads while they are waiting for I/O.
Hence, GIL prevents multi-threading for programs whose threads are CPU-bound, which slows down the execution.
You can actually achieve multi-processing and concurrency in Python with the help of modules like multiprocessing, but it is difficult compared to other languages like C and Java.
How Can You Make Python Run Faster?
There are multiple variants of Python available. CPython is the slowest among those as it does not have a just-in-time (JIT) compiler. Unladen Swallow is a project which targets adding a JIT compiler to CPython. The success of this project will increase the runtime speed of CPython massively.
Choosing Jython or IronPython over CPython can make your runtime faster to a reasonable level. They have heavily optimized virtual machines in the backend (JVM and .NET CLR), which speeds up the runtime to an extent.
We can provide different interpreters for each process to run so that a single thread is provided to each process in multi-processing. Thus, it enables multi-threading and increases the runtime speed.
Many times, the slowness of the code could be because of some other issues. So make sure you use proper data structures and optimize your code.
Final Thoughts
You can’t compare Python with C/C++ or Java in terms of speed and say it is not good. It is like comparing an F1 race car with a utility truck. Their use cases and working mechanisms are different.
Speed is not everything when you choose a programming language. If speed is everything, everyone would be using Assembly or C to write code.
In the case of Python, there are some significant reasons to give up some speed in code execution because you get a lot of flexibility, easy syntax, and reduced development time in return.
It is best to do the groundwork and choose a programming language that suits your problem statement.
I hope you found the article useful. If you have any other points to add to this discussion, comment down below. Happy coding!