Is Python Really Very Slow ? 2 Major Problems Which Makes Python Very Slow
So is Python too slow?
Now the reality is Python is a slow programming language when we look at other languages like Java C and C++. But it’s embarrassing how much faster they can do things than Python. In fact, specific algorithms and applications can actually do things about a hundred to two hundred times faster than the native Python language.
Now before I get too far it is worth noting that would python lacks and speed it makes up for in development time and cost typically developing programs in python is much faster simpler and ends up costing less as there’s less labor involved with the project.
Now, this is a massive advantage in many cases you can actually write you know a Java whip-like code in Python, and about four or five times faster than you’d be able to do it in that language.
so why is Python slow?
Now there’s a variety of different reasons why this language is slow but I just want to mention a few rumors here the main reason this language is slow is not the global interpreter lock although that definitely is a factor in the speed and the way that you can write you know faster Python programs that is not a fundamental reason why python language is slow.
1. Python is Dynamically Typed
The reason the language is slow is that it’s dynamically typed now we’re going to talk about this more in detail but I want to comparison to a language like Java which I actually personally write in.
Now in Java, everything is statically typed and this language is actually compiled before it’s run unlike Python that’s compiled at runtime through an interpreter now what happens in Java when you write code is you need to define what type each of your variables is going to be what type you know your methods and functions are going to be returning and you pretty much have to define exactly what everything is going be throughout your code now although this leads to you know much longer development times and takes a much longer time to write your code what it does is increase efficiency exponentially when you’re compiling now the reason this actually works and the reason this works so much faster than Python code is that if you know the type that a specific variable or objects going to be you can perform a ton of different optimizations and avoid performing a ton of different checks while you’re actually running the code.
Because these checks are performed at compile time in Java essentially you can’t compile any Java code that hasn’t actual or you know even just like typed errors while you’re writing that code so you’re going to try to compile it and it’s going to say you know this type isn’t accurate you can’t do this you can’t compile it because it knows that when it comes to the runtime that’s not going to work.
Essentially all of these checks that actually need to be performed in Python when the code is running are performed beforehand and there’s just a ton of optimization done because of this statically typed length.
Now you might be asking well why doesn’t Python do this?
Well, Python is dynamically typed which essentially means that any variable can change its type and can change its value at any point in the program while it’s running.
This essentially means that we can’t actually compile the entire program beforehand because we can’t do all of these checks at once because we don’t know what type of these variables are going to be.
They’re going to change at runtime different things are going to happen and because of that you know we can’t get all this optimization that we might have in a lower-level language like Java C or C++ and that is kind of the fundamental reason the language is slow.
This typing this dynamic typing and any fast language are going to have a compiler that’s going to run through its going to make sure that everything is good it’s going to do all these checks before it actually ends up running the code at runtime where what happens in python is all of your code is actually compiled and checked at runtime.
2. Lack of Concurrency in Python Makes it Slow
If you’re writing an application in Java C sharp you can kind of spread everything throughout multiple threads and what this allows you to do is utilize all of the cores of your CPU. So to kind of break this down in modern-day computing most of us have four core CPUs or higher and that allows us to actually run for tasks at the exact same time concurrently. Now with Python, this isn’t possible, because well for each interpreter we can have at most one thread running at a time and a thread is operating on a single CPU core. So that means that even if we create a ton of different threads in our Python program we can only be using one CPU core, we know a Java program or a C program could be using all eight or could be using all four which is obviously going to lead to you know a 4Xincrease in speed now we can get around this in Python by using something called multiprocessing.
Now a lot of you are probably wondering why the global interpreter lock exists in Python?
Well, this boils down again to the dynamic type of Python so the way that memory is actually managed in Python and I’m not an expert on this and any means I’ve just kind of looked up a few things and made sure I kind of had an idea what I was talking about is that it’s not thread-safe.
Now what that means is that if two threads are to separate you know pieces of code are trying to access one specific object in memory at the same time you’re going to run into issues and essentially we can’t allow that to happen.
So what we do is we say at least what pythons did is said well we’re going to have a global interpreter lock that means that only one thread can run at a time to prevent this from happening because we know one of the main issues with running multiprocessing and multi-threading applications is you have a deal with locking and sharing memory and that is kind of one of the things that I’m going to get into now with the multiprocessing module in python.
So although concurrency is possible in Python by using the module called multiprocessing which essentially allows you to spawn another Python interpreter that can run its own threads it’s very difficult to actually achieve effectively.
This is difficult to do and it’s not you know very it’s not intuitive to actually get working so that’s kind of one of the issues with it and even though you can do this you still run into a lot of the speed issues with Python coming from the way that it’s interpreted and the way that it works you can’t actually achieve multiprocessing and concurrency in Python but it is difficult and you have to deal with much more difficulties and kind of things getting in your way than I would say in Java C and other languages like that where you can just you know make threads and they will run automatically on the other cores without having to define shared memory objects and locks and stuff like that.
So other than Multiprocessing how can you really increase the speed of python?
Well, one way to do this is to actually use C code as an extension to our Python library or whatever it is that we’re creating. Now python is really built on top of C a lot of people don’t know this but there’s a lot of you know functions and things that you’re actually using that are written natively and C and that Python kind of just has an extension for that allow you to use them and that’s why for example the sorting algorithm in Python will run you know much faster than if you write your own just native sort actually in Python we have a lot of optimizations done and a lot of things written lower-level in know.
This actually means that you can do this yourself so if you need to create something that’s going to run very quickly in Python and you can’t use a different language what you can actually do is write that algorithm and see and import it into your Python code as you know an extension so you can run that code faster than if you had just written it natively in Python.
To actually increase the speed of your Python code you can introduce some kind of threading and concurrency you can use the multiprocessing module which will allow you to have multiple interpreters running at once and get past that global interpreter lock but you may run into some issues with shared and locking memory.
You can also write C extensions for your Python code obviously this isn’t going to be for everyone as you need to know C to do that but if you’re really in a pickle you need to get something fast and you need to use Python for it writing something and C and importing that will speed up your code by a drastic amount of time especially if you have like searching or sorting algorithms or things that are just really intensive computationally if you write those inside that’s going to speed up the execution of time of those exponentially and that’s going to save you a ton of time and ton of headache rather than trying to learn you know an entirely new language when you can just kind of pick out some pieces of C and write a small extension for your Python code.
In a nutshell
So to sum it all up here python is slow mainly because of the two main reasons. One is dynamically types language which means, unlike in java, python has no variable declaration and this makes it quite long to compile and sometimes the variables get changed during the run without our knowledge. Another is python uses a single CPU core to run one thread in an interpreter no matter how many threads you have created in the program.
Now, this leads us into well how can we actually make Python usable and much faster now Python obviously is usable and in a lot of cases you don’t actually care a ton about the runtime of your code or it would be nice if it ran faster but at the end of the day you know a few milliseconds isn’t going to be drastic to whatever application or product you‘re building and in this case, Python is great because it’s much faster to develop and to write code in and typically a lot easier and more readable when you’re developing a project.
So with that being said, that has been why Python is slow how to make it a bit faster and just some information about the language. Please don’t hesitate to leave a comment down below and let me know just like you guys I’m learning as well and this is my kind of surface-level understanding of the Python language.