How to Make Your Loop Way Faster in Python

Python slow? Heres how to make your loops way faster

image

Python has a reputation as a sluggish programming language. Although Python is slower than other languages, there are various strategies to make our Python programs run faster.

How? Simply said, you should optimize your code.

Not only will we get the job done, but we’ll also make our Python code run quicker if we write code that uses less memory and storage.

I discovered a few of days ago a fast and also a super-quick approach to loop in Python.

Normal Loop

Let’s say we wish to add the digits 1 through 100000000. (we might never do that but that big number will help me make my point).

Creating a variable tsum=0, looping over a range, and incrementing the value of tsum by I on each iteration is a common strategy.

import time
s = time.time()

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

print(f'Sum: {tsum}')
print(f'For loop: {time.time() - s} seconds')

This does the task, but it takes 12.45 seconds.

Although it doesn’t appear to be sluggish right now, when you add more 0s to the number inside the range, it will get slower.

Let’s get things moving!

Faster way in basic Python

Using built-in functions in Python provides a quicker approach to loop.

We might use the sum function instead of the for loop in our example. This function sums the values inside the specified range of integers.

import time
s = time.time()

tsum = sum(range(100000000))

print(f'Sum: {tsum}')
print(f'Sum/range: {time.time() - s} seconds')

It takes 2.54 seconds to run the code above. That’s a lot quicker than the last loop! This is why we should avoid using loops and instead use built-in functions.

There is, however, still potential for development.

Stupid fast using NumPy

Optimizing my code is one of the software engineering techniques I should follow to become a better programmer, I learned.

Loops can be made more efficient by vectorizing operations. This is a factor of one or two quicker than the pure Python alternatives (especially in numerical computations).

We can use NumPy to achieve vectorization. Numpy is a package that has efficient data structures for storing matrix data. Because it’s mostly written in C, you can bet on it to be fast.

Let’s have a look at the Numpy techniques. Instead of using Python functions, you may use .sum and .arange.

import time
import numpy as np

s = time.time()
tsum = np.sum(np.arange(100000000))

print(f'Sum: {tsum}')
print(f'Duration: {time.time() - s} seconds')

This completes the task in 0.703 seconds. This is far quicker than the previous methods.

This is why, wherever feasible, you should choose vector operations to loops.

Some more torture for my computer

So far, we’ve seen a simple Numpy program, but what if we need to do more than just a for loop and an if condition?

Numpy definitely beats loops in this case.

Consider the following scenario: we have a set of random exam results (ranging from 1 to 100) and we want to calculate the average score of those who failed an exam (scoring 70).

With a for loop, this is how we’d accomplish it.

import time
import numpy as np

rscores = np.random.randint(1, 100, size=100000010)

s = time.time()

cfailed = 0
sfailed = 0
for score in rscores:
	if score < 70:
		sfailed += score
		cfailed += 1

print(sfailed/cfailed)
print(f'For Loop: {time.time() - s} seconds')

It takes 38.25 seconds to do this task. Not bad, however NumPy gives us quicker results.

This is how we’d implement it in NumPy.

import time
import numpy as np

rscores = np.random.randint(1, 100, size=100000010)

s = time.time()

mean_failed = (rscores[rscores < 70]).mean()
print(mean_failed)

print(f'Numpy: {time.time() - s} seconds')

It takes roughly 1.57 seconds to run the code above. That’s a lot faster, and the code is simple!

Enjoyed this article?

Share it with your network to help others discover it

Continue Learning

Discover more articles on similar topics