DEV Community

ashwinigajji for Python Discipline @EPAM India

Posted on

Ways to Speed Up Python Code

Author: Ashwini Gajji

Read time: 9 mins

Python is easy to learn, has an excellent selection of open-source libraries, and has an extremely active and helpful community.

When it comes to working with large quantities of data, Python can be really slow. Compared to working with languages like C and C++, Python can feel too slow at times. There are some fantastic libraries and built-in functions that can speed up Python code.

Prefer List Comprehension Over Loops

List comprehension is an elegant and better way to create a new list based on the elements of an existing list in just a single line of code.

List comprehension is considered a more Pythonic way to create a new list than defining an empty list and adding elements to that empty list.

Another advantage of list comprehension is that it is faster than using the append method to add elements to a python list.

Example:

Using list append method:

Image description

A better way using list comprehension:

Image description

Code looks cleaner when using list comprehensions

Use Multiple Assignments

To assign the values of multiple variables, no need to assign them line by line. Python has an elegant and better way to assign multiple variables.

Example:

Image description

A better way to assign these variables is:

Image description

This assignment of variables is much cleaner and elegant than the above one.

Proper Import

Avoid importing unnecessary modules and libraries until and unless we use them. Specify the module name instead of importing the complete library. Importing the unnecessary libraries will result in slowing down code performance.

Example:

To find out the square root of a number. Instead of this:

Image description

We can use :

Image description

Function Calls Are Expensive

Function calls are expensive in Python. While it is often good practice to separate code into functions, there are times should be cautious about calling functions from inside of a loop. It is better to iterate inside a function than to iterate and call a function each iteration. The following example where we want to create a list of cubed values in the range of 1–1,000,000.

Image description

Image description

We can see that the second method was faster with all things equal other than using a function to cube the value. Functions have a lot of overhead, so for simple tasks like this they can add a lot of time relative to the total.

Using Built-in Functions and Libraries

Python’s built-in functions are one of the best ways to speed up code. Use built-in python functions whenever needed. These built-in functions are well tested and optimized.

The reason these built-in functions are fast is that python’s built-in functions, such as min, max, all, map, etc., are implemented in the C language.

Use these built-in functions instead of writing manual functions that will help you execute code faster.

Example:

Image description

A better way to write this code is:

Image description

Here we are using the built-in map function, which is written in C. Therefore, it is much faster than using a loop.

Proper Algorithm & Data Structure

Each data structure has a significant effect on runtime. There are many built-in data structures such as list, tuple, set, and dictionary in python. Most people use a list data structure in all cases.

In python, sets and dictionaries have O (1) lookup performance as they use hash tables for that. Use sets and dictionaries instead of lists in the following cases:

You do not have duplicate items in the collection.

You need to search items repeatedly in the collection.

The collection contains many items.

Avoid Global Variables

It is usually preferable to use local variables to keep better track of scope and memory usage. But beyond memory usage, Python is also slightly faster at retrieving local variables than global ones. So, it is simply best to avoid global variables when possible.

Vectorization in Python

Vectorization is a technique of implementing array operations without using for loops. Instead, we use functions defined by various modules which are highly optimized that reduces the running and execution time of code. NumPy being a C implementation of arrays in Python provides vectorized actions on NumPy arrays.

Method that adds elements using for loop and its Computation time:

Image description

Method that adds elements using vectorization and its Computation time:

Image description

Here we can see NumPy operations are faster than for loops.

Max of array

For finding the maximum element in an array, we can use for loop as well as python built-in methods max () . Let's compare ways with NumPy operations.

Max () operation using for loop:

Image description

Max () operation using built-in method max ():

Image description

Max () operation using numpy :

Image description

Here we can see NumPy operations are faster than built-in methods which are faster than for loops.

Conclusion :

In this article, we have discussed some tricks that can be used to make python code run faster. These tips can be used especially with competitive programming where the time limit is everything.

References:

https://www.youtube.com/watch?v=-Wks3CLHtsA

https://towardsdatascience.com/10-ways-to-speed-up-your-python-code-e3d57630b710

https://www.kdnuggets.com/2021/06/make-python-code-run-incredibly-fast.html

https://www.geeksforgeeks.org/vectorization-in-python/

https://www.askpython.com/python-modules/numpy/vectorization-numpy

Disclaimers:

This is a personal blog. The views and opinions expressed here are only those of the author and do not represent those of any organization or any individual with whom the author may be associated, professionally or personally.

Top comments (0)