Python is slower and 'heavier'. Why should you care?
Your users don't really care how much faster your application is. They care if you can give them what they want in a time that matters to them. They also don't care how much memory or CPU you are using to run their processes. They care if you are giving them the data they want when they want it.
In all those years meeting people that want to learn more, want to be developers or just learn how to program, they always ask me this question: "What language should I learn?". The usual answer is Python.
Some linguistics ramblings
We - as humans - desire to communicate with machines the same way we desire to converse with our peers. To that end, we try and learn different languages. The thing that is often overlooked is how the language means quite little when learning it; they are not just the syntax and symbols. If you are bilingual and have lived in a country where the other language is spoken, you have potentially experienced what I am getting at: the cultural difference.
We might understand the symbols and even the syntax. But languages and cultures mean more than that. Once we understand this, languages are the channel for the intent. A prime example is the English language. Many countries use it as their main language, but some expressions and ways of implementing it are quite different.
Similarly, when learning a computer programming language, you will have to encounter 'cultural' changes that can make lots of differences on how you interact with the machines. Examples are interpreted languages, compiled languages, languages that handle memory by themselves and languages that leave you the memory handling decisions.
A strong example of computer cultural difference would be the error handling or not handling of a language/framework. A language like Golang does not have "exceptions" where it handles what I like to call "stack explosion". Whereas Python can catch those up the stack if you want to just leave the code to do what it needs to. It does not seem like much, but those are such different paradigms that some cannot believe the approach of the others.
This difference in error handling philosophy is a key cultural difference. Golang forces you to explicitly handle errors. You return them as part of the function's result, and the caller is responsible for checking them. This leads to more robust code, as errors are not silently ignored. Python, on the other hand, often relies on exceptions, which can be easily overlooked or mishandled, leading to unexpected crashes. While Python's exception handling can be powerful, it requires more careful design and testing to avoid issues. The choice between these approaches is a matter of preference and the specific needs of the project.
All of this to say: Python is a computer language, and all languages have their own cultural aspects and approaches. It will be able to communicate your intent just as well as any other language, if you are fluent in it.
Programming Language Differences
Which one is better? Well, it depends.
If you need to keep your memory footprint minimal, then you potentially need to manage every single thing you want the computer to record in their RAM (Random Access Memory).
If you need to make something that works in little time, and have no time to create a bigger structure that will define every single hardware decision, then you potentially need a scripting language or an interpreted language.
Here is where things get way more complicated, there are not only one or two languages of those kinds, but lots of them. Each language was created with something specific in mind, and as such, something that 'feels' good using it for. Some would say that Perl is incomprehensible, I say they are crazy and this is the only Bubble sort you need:
for($i=$#a;$i>0;$i--){$m=$a[0];splice(@a,0,$i+1,map{$s=$m;$m>$_?$_:($s,$m=$_)[0]}@a[1..$i],$m);}
But I digress. You can probably already tell that there is a language for everyone, and many of those are interchangeable. Some will be more efficient in resources, while others are more efficient in number of words to get something working. Which is why I dare ask: Is Python really that slow? Again, it depends.
A Practical Approach
As an example, I have written many Python projects, Golang Projects, and recently Rust projects. I wanted to know how much that would really be different. I don't mean a theoretical sorting algorithm and see how much memory/cpu/time it takes to make that calculation. I mean an API, Kubernetes Operator, Bot, etc. A project you would actually want to write, not the benchmark alternatives.
To do so I have written a Discord Bot which manages a few aspects of my interactions with friends. I have written both in Golang and Python the same bot with similar functionalities. Both using the discord libraries for registering commands and then just handling their interactions using the same old algorithm - just translated into each language.
The Python project is running using discord-py for the discord interactions, aiosqlite for SQLite DB management and posthog for collecting event logs. A bit of pydantic to ensure my models are looking good for handling the data coming in and out of the bot, httpx to request a backend API in some cases to check a status of another service.
The Golang project is using similar libraries, but their golang counterparts: discordgo for Discord, go-sqlite3 for SQLite and posthog-go for Posthog.
The projects are quite different. The python project was created with 13k lines of code - MANY analytics things and functions that the golang counterpart does not have:
nu ⌁ : ls -al **/*.py | where name !~ "tests/" | each {$in.name | open | lines | length } | math sum
13716The golang counterpart with only the parts that are important for the code to run, and reusing the views from the python implementation and not really being feature complete:
nu ⌁ : ls -al **/*.go | where name !~ "tests/" | each {$in.name | open | lines | length } | math sum
14866
It is an understatement to say that my code in python is more feature complete and robust than the Golang counterpart, I wrote it over the course of 2 years whereas the golang project was just a conversion of the MVP that took around 5 months.
To analyze the data, both projects were deployed in kubernetes pods to be deployed in the same node and network. The metrics are then saved in my system of record and then I'm just drawing an average conclusion to simplify things here.
Here is what I have learned
CPU - The brains
The CPU usage of python was higher. During my avg testing of the bot, it took 10-30 mCPU in my kubernetes cluster to handle my actions. Whereas golang took only 5-10 mCPU. That means that my python code was at best 2x slower and at worst 3x slower.
Memory
The RAM usage of both are not as different as you would expect. To my surprise the Python implementation used 300-450 MB of RAM, whereas the golang implementation was using 200-300 MB of RAM. That was the biggest surprise to me, given how much I was handling memory translations between my references and definitions of variables, quite a surprise.
Time
This is where things get more interesting. While I do love the approach-ability of developing and delivering Python code that will be running everywhere, there is no denying that my golang compiled project took MUCH less time to respond.
On average my Python bot was responding within 10-120ms, depending on the operations. Here is where the difference was dramatic: The Golang implementation responded within 100 or 200. Microseconds.
That's right. Microseconds. While using less memory and CPU. This is the real power of something compiled. Even though it still uses a garbage collector, and I probably was not doing a decent job translating the code to ensure that things would be as efficient as possible, it still was an order of magnitude faster.
The size
As both containers are being built and pushed into the cluster, sometimes it is important to understand the size of the final container images. That makes a world of difference when you have to pull, store and build your code to be used on your clusters. The python container comes at around 120 MB with all dependencies.
The Golang container image comes at an impressive 35 MB. It does not seem like a lot, but if you have a limited link to your container registry, it could make a world of difference to deploy a smaller footprint image. As well as the cost of storage.
Some of our clients are surprised how much cloud providers can charge for the storage and maintenance of their container images and their older versions. If you can reduce the space used per version of your image, it can make a great difference in your bill at the end of the month.
Conclusion
Yes, Python is much slower, and 'heavier' on your machine. It will take longer to respond, it will take more memory out of your machine, and it will use more resources out of your CPU. But ask yourself: Should you care?
Here is the important information out of this entire post: Nobody noticed.
Between the two implementations, the only thing that my users noticed was that my bot would die because of a panic I did not handle properly in my golang implementation of the bot. Which I promptly reverted to the python implementation and as expected: again, nobody noticed.
This is what you should think: it is not the language and it is not the algorithm. It is not about the resources you use or how fast you reply. It is the application itself that matters.
Your users don't really care how much faster your application is, if you are using much less memory or CPU. They will care if you can give them what they want in a timely matter, if you are giving them the right data and that you are available when they need you.
YOU Should NOT care whether python is heavy or slow. You should care what your users are going to see and if you can live with the decisions you have made to make it happen.
Use whatever language you understand and can work with. Make it as efficient as you need it to be so you don't have to worry about handling servers of the size of a building. Just make sure you are making things that your users love to use.
If you are learning now, I truly believe the logical approach of Python is the best for learning. It is less error prone in logic than JavaScript, but packs a punch for most things you want to do.
What are your experiences with different languages? Have you also felt that Python is "fast enough"? Share your thoughts and examples in the comments below!