5 tips and tricks to speed up your Python programs

Contents

Introduction

A Pycon 2021, the creator of the Python language, Sir Guido Van Rossum drew up his long-term and short-term plans on how to make future versions of Python faster and he envisions doing it twice as fast as it currently is.. PyPy and CPython are some of the existing examples that try to increase Python's execution speed, but you can also do it yourself if you just follow some tips and tricks on how to improve your coding skills so that you are writing efficient code and not wasting memory or CPU time.

86747image1-6962105

Then, Why choose python?

Python has become one of the most accepted languages ​​due to the fact that it is easy to program and very convenient to use.. Speed ​​was never one of Python's strong points, but it doesn't mean you always have to give inefficient results. In python people prefer Development speed ” about him “Execution speed“. May not have the raw performance of C or Java, but you will be surprised how fast a properly optimized Python application can run. This speed is sufficient to power various applications such as data analytics, automation tools, administration and many others. If you follow some standard coding tips and procedures, you might almost forget you were trading app performance for developer productivity in the first place.

1. Code Profiles

16872image202-8347389

In reality, you need to measure your code outside of your test or UAT environment and directly into production or live environment to find out exactly why or where your code is running slower. This is where profiling comes in and you can use Python's built-in software “CProfile” module to inspect your code for parts that may be reducing the performance of your general code.

If you need higher precision, you can use any other powerful profiler which will provide you in depth analysis, but in many cases, a simple profiler can go a long way in tracking down the culprit function or line of code that is causing a bottleneck. in execution. You will be able to accurately identify and test with a baseline to establish abnormal patterns or execution times in various deployment scenarios and you can try to guess prematurely, but that may not lead to much success and, Thus, you should always use a profiler to break down your code efficiently.

2. Memorization

25473image203-5019033

Memorization is a process in which the same work is not repeated over and over again, even if it is a function whose value has already been calculated previously. Python gives you the option of a cache that will give you the ability to instantly get results from functions that have been previously calculated. These special Python functionalities are known as decorators and you can use them in your code to speed up your code..

Input :

import timeit

def fib(n):
    if n < 2:
        return n
    else:
        return fib(n-1) + fib(n-2)

t1 = timeit.Timer("fib(40)", "from __main__ import fib")

print(t1.timeit(1))

Departure: (on my local machine)

47.392615799999994

Add the following two lines of code:

from functools import lru_cache
@lru_cache(maxsize=100)

Departure: (on my local machine)

5.8000000000002494e-05

Shocking, It is not like this? This is the power of LRU_Cache in the function library. You can set a custom value for the LRU cache or set it to 'None’ to store everything. Used to store frequently repeating values ​​within a stipulated period of time. For instance, perhaps the most recently retrieved items during the last 24 hours or a value that will be called multiple times over the next 24 hours.

3. Use Numpy for all math operations

32203image204-7190979

NumPy is famous for its mathematical operations based on matrices or any type of matrix, but the secret ingredient here is that it stores numeric data more efficiently than Python's built-in data structures. The package has many replacements for traditional math operations and, since it uses the C libraries, it is much faster than general python functions. NumPy also efficiently manages its memory for very large data structures, like lists with a million items.

Numpy can optimize and save up to 75% more space than normal Python lists. NumPy Array is the only thing that is close to arrays like in Java or C and is easy to declare and use and, Thus, adheres to the initial commitment of “Development speed”.

4. Always use a C library whenever possible

93008image205-3986538

As you have already seen, the use of NumPy in conjunction with the C libraries and how powerful it can be. A similar concept can also be applied to other libraries and functions. Python libraries are not as efficient as Cones and, Thus, if you have the option or opportunity to use a C library, always choose C library. They are faster and more efficient than their respective Python libraries. Python Ctypes Library is an excellent example that is compatible with the Python runtime and takes advantage of the benefits of C.

You can get the best results by reducing the number of trips from C to Python, since passing data between them is an expensive operation. For instance, consider two scenarios where you are passing one value at a time in a loop to C from Python, calculating and returning it and, in another case, pass a list to C, perform your calculation there and send the result back. Always choose the second option, since it is much faster and more efficient in this method.

5. Know your library and its functions

74463image206-5943734

It is very easy to include all the libraries you know in a code and you will never have to worry about importing anything other than in practice, that's one of the worst ways to code. It is analogous to ordering too much food than you can usually handle and, in the end, you will feel tired and difficult to finish. While Python can be a powerful language, it is no excuse to accumulate all the existing libraries in it just to avoid errors. However, you are corrupting your own code if you are importing more than required libraries into your python code. Always keep in mind what functions you need and what libraries are additional. Remove the line or comment them out to remove that overhead and your code will run faster.

Final notes

Now you know some tips and tricks on how to make your code faster and more efficient using a few simple techniques and if you have more questions, feel free to contact me at LinkedIn or if you like geometric brownian motion and the stock market, you can see another of my articles here. Stay safe and have a nice day.

The media shown in this article about 5 tips and tricks to speed up your Python programs are not the property of DataPeaker and are used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.

Datapeaker