Monday, January 25, 2021

How slow is Python?

 I know that people who use Python love it more than anything else. It is easy to run, works everywhere, easy to deploy, easy to understand etc... I'm coming from Assembly/C/C++ tradition (or should I say tribe) which values speed over the ease of use. For that reason alone, I love Assembly and C, but I'd rather use C++ because of templates, constant expressions, stl and such. During the pandemic, I did what I love most: self-education. I attended government backed online courses and applied to a new university after my bachelor's degree. No, not one. Three. Yes, I'm now handling three universities (two of them are remote learning facilities) which one of them is something I was yearning to finish: a Master's degree and it is not a remote learning institute. I am a student now and I'm studying for my Master's degree (yes, I use my student public transportation card).

Also, during the pandemic, I decided to learn new languages as I'm cooking and baking at home. I started with Python. Python is really easy compared to most other languages. After attending to "Python and Tensorflow for Data Science" course at the magnificent BTK Akademi (government backed high quality education I mentioned before) I got the basics of the language. It is elegant. Really easy as it's advertised by the most. But it comes with a price: speed.

How would you benchmark a new language? I usually write a little program to display a time result compared to something I know well. I decided to print prime numbers up to ten million (in fact I request the upper limit from the user). I use "time" under my Ubuntu to measure the running time. Running the code under Jupyter Notebook took more than I would cope with. I decided to run it up to a million then. I ran a little script from the command line:


The code is easy (as I advertised, even I advertised that, remember). Here is a screenshot of the Visual Studio Code that I used to display (it is way sexier than the kate, or any Karen by the way).



I am known to be a patient guy. Even I get bored of the waiting for the process to end. It runs and runs and runs. Here is the result:


I knew that there was something wrong with it. I decided to write the same algorithm with the C++ language, using the same logic as much as possible so that the comparison would be ethical (you know the rant over the language comparison blogs, those guys and gals are as fanatic as football fanatics). Here is the (comparably) same code written in C++:



I am not saying that this approach to find the primes is the ultimate way to find the primes. It is an optimized code for the job. The sole purpose of this code is to benchmark two languages. Not to find the best way to find the prime numbers of any sort. Here it comes, ladies and gentlemen, and the winner is:


Yes. Python finished the same job in 4 minutes and more than 5 seconds. For the C++, it took merely 20.5 seconds. 8% of the former. More than 12 times faster.

Will I use Python? If the job requires it, yes, I am fluent with it. Would I use it as my primary language? No. Definitely not. It is slow as hell. No way!

1 comment:

A Survey of Body Area Networks