Skip to content

5 Tips to make your Python code run five times faster

    Although Python is one of the world’s most popular programming languages, it is not without flaws. The most significant? Most likely, it’s a lack of speed. CPython converts Python code to bytecode, which is then executed by an interpreter.

    Is it simple to grasp? Yup. Speedy? Nope.

    Until you edit your source code, the bytecode is cached in an a.pyc file; the first run is always a fraction longer because CPython converts it to bytecode. The second and subsequent runs are better for judging speed.

    Here are five ways to speed up your Python code five times than normal:

    1. Be Pythonic

    Coming from another programming language, it’s very easy to write code that goes against the grain. If you use for loops or arrays, your code will run slowly. The Pythonic way of doing things makes use of features such as map, list comprehensions, and generators. Also, don’t overlook useful functions like sum and range.

    2. Make use of memoization
    This appears to be more complicated than it is. If the cost of calling a function is high, adding “memorization” means that the results are cached; subsequent calls with the same parameter values are pulled from the cache rather than recomputing. This can result in significant speed increases.

    The functools package includes a lru cache function that you can use to decorate the function you want to remember. In the following example, fib is a simple non-memoized fibonacci function, and fib(35) performs many additions; mfib is the memoized version.

    3. Write in C

    This is not always easy to accomplish. You must be familiar with C and how it interacts with Python. Furthermore, there may be only a few cases where coding in C will suffice. The fact that CPython is written in C helps.

    The ctypes library contains a collection of C types and their Python mappings. This library also allows you to call into the operating system libraries, but you should be comfortable working at a low level and familiar with C, including arrays, structs, and pointers before proceeding.

    4. Python machine compilation
    The code that you generate when you compile code will always run faster than interpreted bytecode. Python compilers are available, including Numpa, Nuitka, pypi, and Cython. Optimize your Python code before attempting to compile it. Numpa’s compiler is JIT (Just-In-Time) and supports GPU acceleration.

    5. When possible, deploy ‘From’
    It’s far too easy to rely on import package all of the time. However, it makes more sense to use from when you can simply import the required function (s). Why would you import twenty functions when you only require one?

    Also, keep in mind that you can place imports inside functions so that they are only called when necessary.


    If you only use one method, use memoization to get the most speed out of Python—but you’d probably become a better Python programmer if you learned the pythonic approach. It’s intriguing to see plans to speed up CPython by up to 5x over the next four releases.

    Another proposed method for increasing Python’s speed is to remove the GIL from multithreaded code (Global Interpreter Lock). This is a mutex that allows only one thread to control the Python interpreter. Python variables and objects cannot be accessed if you write multithreaded code that does not use the Gil.