How to make your python code faster?
For that, we should know what makes python slow.
Variable Declaration
- In python when you declare a variable it creates a Pyobject in the memory which has some properties like data stored and reference count.Reference count is used for garbage collection(Slow and blocking part).
- Updating a variable works as follows:
a = 1 # new Pyobject named a
a = ‘somestring’ # new Pyobject created
Reference count of old Pyobject is decreased to 0 so it can be collected by the garbage collector(slow part).
Solution:
Using as low number of variable as possible.
x = result
return x
Fix could be returning x.
Loops
- loops in python are slow.because of large number re declaration of variables.
for x in range(10):
In every loop we redeclare x and old value is need to be garbage collected.
Solution:
Use builtin methods for processes that are possible with builtin methods because they are implemented in C.
Or use a library like pandas, polars, and numpy etc.They use c|cpp under the hood.
Polars uses rust.
Example:
Using sets to remove copies from an array.
For more information