Python is a super popular human-centric language, written in C, which is interpreted line by line in a top down fashion. The primary goal of the creators of Python is to make it easy for humans to understand.
Interestingly, Python is known for being many orders of magnitude slower than the language it is written in, C. If you were ever curious about this, take a look at this video. Basically, the video poses two test cases of Python vs C++ (as fast as C), whereby we compare how quickly Python and C++ can’t count to a billion and print to console.
The end result? C++ did it in 2.4 seconds and Python did it in 1 minute 52 seconds. And, this is not an anomaly. Most operations in Python are much slower than C++/C.
There is a funny Python meme out there too. The Python meme states, “the most performant way to code in Python is… to not code in Python.”. Basically, there are libraries out there which allow you to translate Python code to C and run it that way, not in Python basically.
And, this reaches my other point…
Table of Contents
Why Python Is So Slow…
One of the main reasons Python is so slow, is, that it is interpreted line by line. The translator, which translates the Python line it is reading to some machine instruction, does not compile ahead of time, but rather just in time for execution.
Take a look at this graph:
Basically, a compiler converts code into machine code which is prebuilt and optimized for output. An interpreter does it during the execution. This is the basic interpreter versus compiler bit, which can be a bit complex. But just know Python interpreters do not know the errors on line 50 if it is on line 1.
The errors you see in your IDE are actually errors picked up by some 3rd party analyzer so don’t think you are getting the errors before you run the Python code. This is a feature of the IDE and not Python. You actually get your true Python errors during execution, line by line, top to bottom.
Compare this to C/C++, which takes the code, converts it to machine code/assembler, and, during the conversion finds errors. The errors are not found by some outside software in this case, but rather by the language compiler itself.
Amazingly, this process of directly running machine code/assembler (which is what C/C++ converts/compiles to), is the most performant way to execute code. This direct access to machine code/assembler by the hardware is what does it.
Python, on the other hand, does the entire translation to a low level version of itself, called bytecode, basically a middle language with machine code, then finally runs the instructions.
Take a look:
Because the computer has no direct access to machine code/assembler, it has to run through bytecode then through a compiler line by line. This massively destroys performance.
Solutions exist for this though. One, is to convert Python to C then compile C. Another is to simply turn Python into a compiled language just like C, by simply using a 3rd party compiler, instead of Python’s built-in one. Basically compile to machine code or byte code then run it. These options exist today and are available for developer who want a faster experience.
In summary of the above sections, here is a checklist of things learned:
- Python’s main purpose is to be human readable and easy to write
- Python is executed line by line, this is called interpretation, which is not compilation
- Python is much slower than compiled languages like C/C++
How Python Has Better Build Tools
In addition to the above, Python has perhaps one of the best build systems in the world. What is a build system? A build system/tool allows you to include previously written code AKA libraries into a language’s code, without more work on your part. Another word for this is dependency management, because you are managing the previously written code, your current source requires.
Online pools of previously written code is available for pulling into your computer so you can write epic Python code without having to recreate many needed parts of your program. You can download a graph maker, math maker, graphics maker, 3d model system, audio system, machine learning library and pretty much anything by simply pulling from this online pool.
Now, every programming language has online pools of previously written code that we can pull from. Java has Apache Maven, C/C++ has Vcpkg, Javascript has npmjs, C#, Rust, Python, they all have one.
And, it is pretty standardized too. You will find all these languages have some specific way of including previously written code/dependencies that is just common practice. What makes Python so special is just how easy it is to pull from its online pools of code.
As a side note, we call these pools of code “code repositories” or just “repositories” for short, and yes, anybody can make a code repository and these build tools can link to it and yea all the ones mentioned here are simply the most popular or standard ones used by everyone.
Many companies keep these together, Apache Maven is maintained by the Apache Group, Vcpkg is maintained by Microsoft and many many more exist which are owned by different companies. What is cool is when a company has a private online pool of pre-built code only available to its employees.
Anyway, the main online pool of pre-written code is PyPi for Python. What is special here is yes, the pool itself is amazingly large, with all kinds of packages. Okay, I was going to say Python’s main deal is how easy it is to pull from the online pool of code. But, actually, Python has such a large number of libraries(pre-written code) that we can springboard on top of, it is just amazing.
If Python had a bad build tool for pulling pre-written code, it would still be super popular because of how vast the coverage of the pool/code repository is. So, not only do the libraries cover a lot of ground but, it is easy to pull them.
How Python Build Tools Pull From Online Code Pools AKA Code Repositories
Okay anyway, back to the build tools. There are two main ways Python developers use dependencies. They use either Pip or venv. Both are easy to use and both are pretty standard. Most developers use both for various reasons. using them is as simple as
Which is just ridiculous, really, it is. In C++/C you have to setup all kinds of dependency configuration files and include them, make sure your environment is setup and do all kinds of different things per IDE.
In Javascript you have to manage a package.json file and learn a much less intuitive configuration format. In Java you have to learn Gradle configuration or Maven XML format.
However, here with Python it is much simpler. Yes, I am exaggerating the simplicity a little bit, but it really is much more intuitive than C/C++, Java & Javascript. Yea, it can be difficult even in Python to get setup, but once you are rolling, you really can pick up traction.
To sum up this one portion, how do build tools pull from these code repositories/pools of pre-written code? They use HTTP requests and basically download the code and include it into your interpreter’s source base(translated bytecode source).
So, the build tool uses a URL, downloads and installs the code basically, that is it. A bit oversimplified but yea that is how it is done.
Why Python Is Portable
Before I end I want to explain a bit why Python is portable. Basically a good majority of the code works on all platforms. Hooks into the Operating System obviously won’t work. But, overall Python has the ability to run the same code on all devices with pretty reasonable consistency.
Why is this? It is because of the concept of a virtual machine. Take a look at this:
Notice, the interpreter has a layer which is the virtual machine. It takes all the information from the pre-written code/libraries, the translated code from the source code and then runs it in an equal fashion across all devices.
How is this done? Basically, the virtual machine takes all the different system calls across all the different operating systems and creates an abstraction layer which makes it all the same in Python but different to the device.
For example, if we have moveMouseLinux(); and moveMouseWindows(); the virtual machine basically takes these two and turns it into moveMousePython();.
This is done by checking which operating system it is and choosing either one in the virtual machine. What is cool is that the virtual machine did this for all the libraries in the language’s online pool.
This makes all the libraries very abstract and consistent, very good. However, if you make system calls yourself you basically make the code platform dependent.
Well, I hope this taught you more about Python.
Happy coding!
Resources
Count to 1 Billion Python vs C/C++: https://www.youtube.com/watch?v=VioxsWYzoJk&ab_channel=TheBuilder
Apache Maven: https://maven.apache.org/
Vcpkg: https://vcpkg.io/en/
Npmjs: https://www.npmjs.com/
PyPi: https://pypi.org/