Python has become one of the most popular programming languages out there, particularly for beginners and those new to the hacker/maker world. Unfortunately, while it’s easy to get something up and running in Python, it’s performance compared to other languages is generally lacking. Often, when starting out, we’re just happy to have our code run successfully. Eventually, though, performance always becomes a priority. When that happens for you, you might like to check out the nifty tips from [Evgenia Verbina] on how to make your Python code faster.
Many of the tricks are simple common sense. For example, it’s useful to avoid creating duplicates of large objects in memory, so altering an object instead of copying it can save a lot of processing time. Another easy win is using the Python math module instead of using the exponent (**) operator since math calls some C code that runs super fast. Others may be unfamiliar to new coders—like the benefits of using sets instead of lists for faster lookups, particularly when it comes to working with larger datasets. These sorts of efficiency gains might be merely useful, or they might be a critical part of making sure your project is actually practical and fit for purpose.
It’s worth looking over the whole list, even if you’re an intermediate coder. You might find some easy wins that drastically improve your code for minimal effort. We’ve explored similar tricks for speeding up code on embedded platforms like Arduino, too. If you’ve got your own nifty Python speed hacks, don’t hesitate to notify the tipsline!

The best trick to make your Python code thousands times faster is to write it in C/C++, Pascal, Rust or any other compiled language…
Aw you beat me too it. This is the answer though.
Python is a reasonable tool and all but sometimes I do wonder if I will ever reach for it outside of a scripting context.
It really depends most of the time the code will be run once or twice. In which case dev time costs from writing C/Rust will massively outweigh the gain.
So long as the heavy lifting is in some library like Numpy.
If it’ll be running repeatedly or in an ongoing fashion then it’s deffo worth the devtime costs.
That will make your program run faster, but not your python code as you didn’t modify your python code or the way it is interpreted.
Python has many advantages over compiled languages. You can test things in real time using interactive terminal and Jupyter notebooks. There are many libraries available that are easy to integrate. Python has many optimized libraries. So even if the interpreted code is slow it can delegate performance critical parts to those libraries.
Porting python code to C/C++ can take a few hours to a few months.
I use both C/C++ and python. C/C++ for embedded devices. Python for PC tools (code analysis, processing log files, generating PDF’s and XLSX documents, regression analysis, code generation, web scraping, build scripts, etc.).
I have used micropython on embedded devices, but I’ve never used that in any serious project.
Every advantage you just wrote also applies to Julia, which is JIT-compiled and much faster than Python. Give it a try some time, it’s a really nice language. :)
C?! If I’m using Python it’s because I’m lazy – I don’t want to be bothered with memory management. Go would be a suitable substitute.
This is the answer. I built a whole computer vision app with python and later rewrote it in C++ for performance reasons. Best decision I ever made. Trying to write high performance code in python is an absolute nightmare.
I checked out the list, and two things are clear to me:
It’s a JetBrains ad, hawking their AI “tools”
Calling time.sleep one time instead of one thousand makes the code sleep one thousandth as much
For a real Python performance tip: Every time you use the . operator to look up a member of an object, that’s a hash table lookup. Make local references to repeatedly-accessed members before loops, don’t do the hash table lookup more than you need to. This can make a big difference, and is used all over the place in the standard library.
object.attr is a hash lookup? Now this is hacking … rewriting code to meticulously avoid a core feature.
It’s not avoiding a core feature, you just do object_attr = object.attr before the loop.