Ways to Speed Up Python Code
Published: 2022-12-22 12:12:02

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vouqep894d5qzey2tu4b.png)



A better way using list comprehension: 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/htqop887j1ss6hzss6ag.png)



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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jugo7uo8ynkqlszdcpr9.png)

A better way to assign these variables is: 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lwpu6llcbrl7ejrfti8z.png)



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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gcr12um06kfvmcj2y4yl.png)




We can use : 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y2lfe29rk4z3isihuuww.png)



            

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aoxgzrrrk6gk8pbxdeyy.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otx0lgtwl3o3sxcf51zg.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/anoqmygi996sbao3z491.png)



A better way to write this code is: 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b09qcg9ev9ga6q1y454k.png)


 

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mcyau2rdni92a4nhjozg.png)



Method that adds elements using vectorization and its Computation time: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4bqle73wzzjwb0vnu9l0.png)


 

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0t3in6evv6iq7p5rhbr.png)



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



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xiiyzgue1xxtpaig1vf.png)





Max () operation using numpy : 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zh4m8e97b71kpigm4p7k.png)

 

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.