So you want to write faster code with Python?

Stop being victim of compiled languages devs and fasten up your code (and maybe stand again those devs)

Something I want to clear out (and let you know)

Hold your horses! compiled languages are gonna be faster than interpreted languages and I am not gonna give you any advice on how to fasten up your Python code than almighty C Language, Rust or Golang.

But Python is a very good language. It’s simple and productive (much more productive than Rust if you are a beginner or even intermediate dev as my GitHub says about me). And it’s an interpreted language which means a smarted runtime engine is running the code as per its speed.

Python is heavily used in Data Science and Machine Learning (fields that I am way far off) as well as a backend for huge infrastructures as well as IT automation (my favourite), networking and security. This is why it was my first language since I was a super cool hacker in high school.

And Python is actually slower than those compiled languages. I know interpreted languages have their own merits, but come on, don’t you want a blazing fast language, without learning those statically typed, compiled time languages like Rust and C? (learn golang by the way, it’s easy and compiled)

Here are my tips to write faster code in Python:

Preface: The lesser the Python code, the faster your code will run.

Use Built-In functions

I don’t care how good you are in competitive programming and how many super secret sorting algorithms you use, use built-in functions. Consider a sorting function, instead of implementing your own, use a built-in sorting function. It’s faster than your algorithm written in Python since these built-in functions are written in compiled languages like C. They are gonna be faster than yours in Python, no matter how good you are at 0(n).

Cython in Some Cases

Cython is a compiled form of Python that converts your interpreted Python code into compiled binary. Compiling means faster execution, but make sure you know what you are doing since it’s not always the case that you want to compile code in the case of interpreted languages.

One thing I admire about Python is that you can just modify it on the go and run it on another terminal tab faster, and when working with data, interpreted languages enhance your productivity. But in case you need speed, jump on to use Cython.

Link to Cython: https://cython.org/

Use Compiled Frameworks

The more the compilation, the faster you go.

When working with frameworks, use compiled frameworks since they are mostly written in languages like C which is super fast. It’s better than an interpreted way of running the code and will fasten up your code. There's not much to say, it’s very obvious by now.

Use concurrency

Make your code to run independent processes in parallel to make them faster. For me, golang provides a super awesome concurrency model, but since we are on Python for now, it will provide you with the huge edge of faster processing.

But make sure you use it at the right place. It’s not always the case that parallel execution will speed up your code. Running processes in parallel has an overhead of allocating resources for both of them, so you need to make sure it’s worth it. If you follow me on LinkedIn, you will know my experience with Golang on getting my code slower with concurrency by using it in the wrong place while solving a Leetcode problem (Post-release date: 21 June 2024).

Use PyPy instead of the original Python Interpreter

Python interpreter is written in C Languages, blazingly fast. Now why use PyPy?

PyPy uses JIT (Just in Time compilation) which Java uses (hmm….). This means that when the code is executed, the compiled compiles the further code which is just after the current executing code. You just need to alias python3 to pypy and it will do its job.

Done! now go and impress your fellow Pythonistas

Now you are pretty much ready to fasten up your Python code. Now you don’t have to hide behind the walls for your slow code, you are free to brag about how faster and productive your code is (but beware of those compiled language devs, golang is productive and faster too).

Have a nice day, bye!