Why is Python So Slow & Does it Matter?
Key Takeaways
The video discusses why Python is slow and if it matters, covering topics such as interpreted languages, global interpreter lock, and dynamic typing, with tools like Python, C, C++, Assembly, Just-in-time compilation, and Cython
Full Transcript
[Music] what is going on guys welcome back in today's video we're going to talk about why python is so slow if it actually matters and how to speed it up so let us get right into it all right so let's start with the very basics first which are going to be central to what we're going to talk about in today's video and the first point is defining what python actually is what is python and a lot of you guys especially the beginners are probably going to say okay python is the programming language and what i'm running on my system so when i type python or when i open up the python idle that is python and that's not exactly true because what python is is it's a language definition a language definition or you could say a specification so this is just uh the structure of the language what you're running on your system is not python what you're running on your system is c python or jython or ironpython or pi pi uh an implementation of the python language uh usually what you're running on your system is going to be c python not to be uh not to be confused with syphon which is a library that you can use in order to speed up your code uh c python is what you're probably running on your system so if you just go to python.org downloads you download the version uh you're probably running c python which is a python implementation based on the c language uh there are also other implementations like jython which is based on java uh iron python i'm not really sure what that is about and pi pi same thing here um but what's important is that there are different implementations and they also differ because one of the three points that we're going to talk about today uh for why python is so slow is going to be um something that you will encounter in c python but not necessarily in jython so this is just something that you need to know uh and the question we need to ask here today is okay if c is a super fast programming language and c python is based on c how can it be so slow how can this happen and python is extremely slow if you compare it to languages like c plus plus or c sharp or c or java python is extremely slow and i really mean extremely slow it's not it's not just a little bit so it's really really slow um and we're going to talk about today why that is i'm going to give you three reasons uh but one thing that i want to mention here right away is that python is very slow when it comes to execution time but it's very fast when it comes to development speed so python is super simple and in a lot of application fields nowadays it is more important to have a language that increases development speed and it's not so much important to always get the highest performance so of course if you're in high frequency trading or if you are programming airplanes or a mars rover and so on you want to have the most efficient code possible maybe you even want to write it in assembly but in most other areas in most areas you're not going to be dependent on high performance and your code is not going to be to have to be optimized so that you get the last milliseconds of performance development speed is way more important how fast can we deliver the product so this is one thing that python is definitely not slow at so when we're talking about that python is slow we say python's execution time execution speed is very slow all right so let's talk about the first reason which is that python is an interpreted language and not a compiled language and i would not say that this is the primary reason for why python is so slow this is definitely a factor but i wouldn't say okay python is slow mainly because it's an interpreted language and not a compiled language it is a factor but i think there are more important reasons as well so the one does not mean that it's the most important reason so when a language is interpreted and not compiled this basically means that we have to translate the instructions to machine code during runtime so let's say we have a basic program here this is just those are just lines of code and let's say this is c or c or a compiled language what we do is we run it through a compiler so this is a compiler this is just a very uh superficial high level explanation here but we have this code it runs into a compiler and it produces machine code so you could say zeros and ones here um so this is just a machine code that the computer understands and once we have that machine code that is already the program so we just run this file for example on windows.exe file and that's it we just execute it and we don't need to do any translations of course we do certain things during runtime but we don't need to do anything fancy like okay this piece of code needs to be translated into machine code because it already is machine code it's already optimized for the computer whereas in python or in interpreted languages in general what we have is an interpreter and this interpreter doesn't interpreter it doesn't translate the whole thing and then just execute it it executes statement by statement it translates statement by statement so if we have something like print hello world then what it's going to do is it's going to transform that statement into machine code and so on and then the next statement is going to be transformed into machine code and so on all over again and in python you know we have a lot of uncertainty so you cannot just optimize it since it's interpreted you cannot always optimize you cannot say okay i'm going to compile the whole program make it as optimal as possible and then you just need to run it no we need to do it all the time during runtime we need to translate the code and this is of course this is of course very very slow and it takes time because compile time is a thing that you have one time so if you have a big program you compile it maybe you compile it for a very long time but once it's compiled it's done you don't need to do anything else you just compile it and you have an executable or maybe multiple executables whatever when you have an interpreted language every time you run it during the run time during the execution time you need to translate the statements which takes time so this is definitely something that's slowing down python also what we don't have in python but what we have in other partially interpreted languages like java for example is that we don't have a just in time compilation so we don't have jit jit is just in time compilation basically means that i have already a tutorial on this channel i think about number you can take a look at it where you can introduce jit into python basically means that you have interpreted code but certain segments for example or maybe the whole thing is interpreted just in time it's compiled just in time so this is the code and then once you execute a certain piece of code it is compiled during runtime and the next time you execute it it's going to execute the compiled code so you don't need to interpret it again you don't need to translate it again but python doesn't have a jit by nature so of course you can use number to speed up certain segments but all in all python is completely interpreted so this is one reason for why it is so slow reason number two is the global interpreter lock or short jill or gil whatever you want to call it and the basic problem here is that modern computers have multiple cores uh and those cores can be utilized by using multiple threads so we have quad-core cpus dual-core cpus maybe even more than that and if you want to make use of that you need to do that with multi-processing or with multiple threats so a process can spawn multiple threats threats share the same memory space they have the the same resources and those threats can then communicate with each other easily and they run concurrently they run at the same time and they process tasks at the same time now what we also have with threads is we have so-called locks and locks are not something bad locks are important for synchronization so let me give you a very very basic example let's say we have some basic resource here this green dot is a resource and then we have um two threads we have t1 and t2 those are the threats now let's say t1 wants to change something in that resource we want to increase for example if this is a number we want to increase it now let's say t2 wants to do that at roughly the same time and we also want to increase it or we want to decrease it we want to have two concurrent writing operations or we're trying to do that at least now let's say we don't want this to happen we don't want two threads or multiple threads to be able to manipulate the same resource at the same time so they can still run concurrently but if they are accessing the same resource it needs to be locked or we want to have this in a synchronized way so what we can do of course is t1 can say okay i'm locking this here i uh activate i require a lock then i'm increasing it then i'm unlocking it again and then t2 is going to lock it and change it and unlock it again so this is a very uh not sophisticated superficial definition and explanation of locks but it's not too important here we use locks in order to synchronize threads now in python or in c python at least what we have oh i have written syphon here by the way c python in c python we have something called reference counting so for each variable we store how many references we have to that variable so we have some variable let me just pick another color here we have some variable a here and let's say we have some references to that variable now once those references are gone and reached number zero this is going to be freed again a very superficial explanation but we don't want to go into too much depth here uh we have this reference counting here and in order for that reference counting to also work when we have multiple threads in c python we have something called the global interpreter lock so basically this means that the interpreter can only process one instruction at a time basically killing multi-threading so since we have that global interpreter lock the interpreter is never able to process more than one instruction at a time which basically means you can start a thousand threads you're still going to only have one instruction at a time because you have this global interpreter lock and this affects cpu bound tasks or cpu bound threats so if we have something where we have a lot of computation concurrently this is going to massively affect performance in fact it's going to not be multi-threading at all it is not affecting io bound tasks so for example if we have uh input that we're waiting for or if we have in general idle time this is not a problem so we can still uh asynchronously do certain things we can simulate concurrency but we don't have actual uh concurrent programming in python because we have this global interpreter log at least in c python which brings me to the last point in jython for example and i'm not sure if this is also the case in other implementations of python but in jython we don't have the global interpreter log because jython profits from the jvm environment java virtual machine so here we don't have a global interpreter log this is just something that you might want to look into if you're interested in that and also what we can do is we can do a multi-processing because multi-processing is not the same process with the global interpreter lock but multi-processing spawns multiple processes and all of these processes have a global interpreter lock themselves those are individual interpreters so multi-processing definitely works so last but not least let us get to the final reason which is python is dynamically typed and in my opinion this is the main reason for why python is so slow in python we don't need to specify statically any data types so when we have python code what we do is we say a equals 10 b equals true or c equals hello and then later on we can even say okay a it was an integer up here it's now going to be a list of strings and booleans and all sorts of things we can mix things up and so on and of course this can be very convenient for programmers it's very flexible we can always change things we can still do type checking manually and we can also do type hinting type hinting is uh not influencing the code it's only influencing the readability uh so it's not influencing the actual uh program in the end but you can say okay we're expecting an integer here we're returning an integer here we're expecting a string here and so on so it's just for readability and documentation purposes uh so you have the predictability on a code level on a readability level if you want to but there's no compiler or interpreter that knows okay this is going to be an integer for sure the types are always dynamically determined during runtime so every time we have a variable a we need to say okay what is a is it an integer okay a is an integer so we're going to treat it as an integer but later on in the code a might be a string or it might be a class uh yeah it can actually also be a class it can be an object it can even be a function it can be everything in python so we're super flexible here and every time we read or we write into a variable we need to do a type check every time and this is all happening during runtime we don't know what it's going to be uh it can change the type even if we figure out okay a is a list of strings it can change it can change and in a couple of lines of code a is going to be an integer so we have no predictability which makes code optimization extremely hard if not almost impossible and this is radically slowing down python because we don't have uh we we cannot optimize the code we have no compiler or no interpreter that can just say okay i know this is going to be that type this is going to be that type this is going to be predictable here so we can reduce it down to that no we don't know what's going to happen we can even determine types during run time so i can even change i can say okay a equals something and i can change the type during runtime and because of that code optimization is extremely hard and we're not going to have um fast code execution because statically typed languages are just faster this is this is just a truism um and flexibility in python comes at cost of the speed everything's dynamic everything is easy and simple and we can do all sorts of things without problems but this comes at the cost of speed at the cost of execution speed finally let us talk about the conclusion is this really a problem or doesn't it really matter and as i already said development speed nowadays trumps execution speed in most fields it's more important because we want to have developers being able to write code quickly to change code quickly we want to have a fast development speed and it doesn't really matter for most things if the execution speed is the best one we can have now of course if you're into 3d game development if you are into low-level hardware programming if you are programming with limited resources if you're programming uh in a field where performance is very important you're not going to use python in general you're going to use c c plus plus maybe assembly uh and for this i have the i want to bring up the finance example because i have read a little bit about how finance programming is done and basically what i read in different articles and i also watch a couple of videos is that in finance what you do is you have python for prototyping models so okay you want to know um this is a new algorithm i came up with this is a new machine learning model that i came up with for stock trading for example technical analysis stuff for example and what you do then is you implement all this in python in order to prototype it so you just um develop the whole thing the whole algorithm the whole model in python and then you try it you backtest it on data you see okay how well would it have performed in the past how well does it perform now but you don't put it into production because it's super slow and in finance everything is about milliseconds you want to be the fastest you want to be the first one so it's good enough to do it in python because in python you can change stuff you can experiment around easily but once you know okay this model is very good this algorithm is very good i want to use it for my stock trading you're not going going to run it on python unless of course you know you're doing something in the long term but if it's if it's high frequency trading where everything is about milliseconds you're going to take this model that you have developed in python and you're going to implement it in c c plus and sometimes even assembly as optimized as possible so this is an analogy or an example here on how things are done if performance is needed python is used for its development speed and if really high performance is needed you're going to use c c plus or assembly in the end so let's talk about a couple of solutions that we have here uh at least ways to speed up python code and i think uh for those two i even have a video on my channel so number just in time compilation you can introduce jit for certain segments for certain functions in python to massively speed them up because you're using just in time compilation this mainly works for mathematical computations and for numpy doesn't work so well with pandas or scikit-learn for example and we also have cython which introduces uh static typing so with syphon we can use c types uh static static typing there you go so with with syphon we can introduce c types into python and if that's not enough we can even program whole c extensions so certain pieces of code written in c executed by python to massively speed up performance so maybe i'm going to do a video on that in the future as well and what i have also read in the internet by but i have no guarantee here that this is actually true uh there is actually some work being done on improving python continuously there are always new python versions and maybe there are speculations that maybe sooner or later um the global interpreter lock will be removed with something more intelligent maybe we're going to find a better way of maybe even compiling stuff certain pieces of code maybe we will have static types in some way by nature even though uh python remains dynamically tight but for now we don't know what's going to happen so if there are some major updates i'm going to make a video on them which is why i want to be subscribed on this channel so if you want to know um new python things so whenever a new python version is released i usually make a video what's new in python 3.9 what's new in python 3.10 3.11 and so on uh so if you want to stay updated you want to make sure that you're subscribed and hit the notification bell right so if there is some update on python it is getting faster and faster because of improvements i'm going to make a video on that all right so that's it for today's video hope you enjoyed it hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you
Original Description
In this video we are going to discuss the fundamental question of why Python is so slow and if it actually matters.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
🎵 Outro Music From: https://www.bensound.com/
Timestamps:
(0:00) Intro
(0:17) Fundamentals
(3:32) Reason 1 - Interpreted
(7:14) Reason 2 - GIL
(11:54) Reason 3 - Dynamic Typing
(14:57) Does it Matter?
(19:20) Outro
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
AI CLI Tools Are Eating Each Other's Lunch
Dev.to · Tracepilot
Loop Engineering: The Skill That Just Made Your AI Workflow Obsolete
Medium · Machine Learning
We built a real-time Pokémon TCG AR overlay for live streams and open-sourced everything
Medium · Deep Learning
I tested the new Claude Desktop on Linux - here's how it compares to rival apps
ZDNet
🎓
Tutor Explanation
DeepCamp AI