Optimize Your Python Loops!

Python has transformed the programming landscape ever since its emergence on February 20, 1991.

Eashan Mahajan
Operations Research Bit
3 min readMar 20, 2024

--

Photo by Florian Olivo on Unsplash

Guido van Rossum developed the coding language with an emphasis on code readability. Because of this, Python is a popular starting language for beginners and is commonly used in various industries.

While Python has received tremendous amounts of praise from users, it has unfortunately received one constant complaint: It’s too slow. Compared to other industrial languages (C++, Java, JavaScript), the complaint holds. However, Python does have a couple of neat tricks that allow you to speed up specific aspects. I’m talking about Python loops

There are ways for you to optimize your loops, and we’re going to discuss a few of them.

Loop in Python

Alright before we discuss ways to make our loops faster, lets look at a basic loop and find out how long it takes.

import time
s = time.time()

sum = 0
for i in range(100000000):
sum += i

print("Sum: ", sum)
print("Time: ", (time.time() - s), "seconds")

While it was able to print out the sum, it took 6.07 seconds to complete. There are certainly ways we can improve this, like a list comprehension.

List Comprehension

A list comprehension is a shorter way of creating lists with pre-existing elements. While you may not be able to replace all loops that involve a list with list comprehensions, we can certainly replace our loop. The code is as follows:

import time
s = time.time()

sum_result = sum([i for i in range(100000000)])
print("Sum: ", sum_result)
print("Time: ", (time.time() - s), "seconds")

This has a time of 4.32 seconds! Nearly 2 seconds faster than using a normal loop.

Built-In Functions

Another way we can speed our loops up is by using built-in functions, as such:

import time
s = time.time()

sum_result = sum(range(100000000))
print("Sum: ", sum_result)
print("Time: ", (time.time() - s), "seconds")

This takes only 0.97 seconds! Much faster than our previous 2 methods. How this works is when the range function generates an iterable containing numbers from 0 to 99999999. From there, the sum function takes the iterable and returns the sum of all the numbers in the iterable.

Arithmetic

Alright now the last method we’re going to cover is an arithmetic series. Now this is for very specific loops, those that use math. In our case, this works. We can use the formula for the sum of an arithmetic series, as such:

import time
s = time.time()

n = 99999999
sum_result = (n * (n + 1)) // 2
print("Sum: ", sum_result)
print("Time: ", (time.time() - s), "seconds")

This returns an output of 3.29 seconds! While not faster than built-in functions, it outpaces a list comprehension and a normal loop.

Closing Thoughts

Well, there you go. Those are three alternative methods that are faster than a regular loop. Now, there are cases where your loop is much more complex and you won’t be able to make it like the examples. However, you can utilize each of the methods in any part of your loop, making it faster and more efficient. I highly recommend optimizing any part of your loop that you can, to speed up your program. For now, that’s all I’ve got for you, and thanks for reading!

--

--