python programming python optimization for loop in python

Python's tales from the for-loop: when performance does matter

Spoiler alert! We will talk about list construction, memory efficiency, function/variable execution and conditions.

A frequent case of condition in a loop that can be avoided is handling the first and/or last iteration differently.

We will start by benchmarking the case with the conditions inside the loop

Illustration by the author.

Then when we handle the first and last element outside the loop.

Illustration by the author.

We can notice the performance gain.

Last illustration won’t contain a benchmark, it is to show you how, when using a generator, you can do a specific processing for the first element, but still have it while iterating the loop using itertools.chain.

Condition’s order can have an impact

When you have independent conditions, that is to say when you can change the order of the conditions in a if — elif case, choosing the order of the conditions can have an impact.

Let’s consider three variables: a, b, and c. When we encounter a multiple of 10, we increment a, when it is an odd number we increment b, and in other cases we increment c

If we consider the first 10 numbers, it would give us:

a = 1, b = 5, c = 4

Let’s benchmark it, shall we?

Illustration by the author.

The first benchmark will be used as reference, the first condition is the one we will enter the less, the second one correspond to the case that will happen most often.

eIllustration by the author.

In the second benchmark, we reversed the two first conditions to have the most frequent case first. We can note a non-negligible gain in performance. Why ? Because by putting the odd/even check first, we needn’t to perform the multiple of ten check for half the cases.

Illustration by the author.

A third benchmark, the odd/even case is still first, but we reversed the logic to have the condition for the next more frequently occurring case (the “other case”, that is to say neither odd neither a multiple of ten).

While it is still faster than the first benchmark, it is a bit less more slow than the second one. It’s because it is faster to perform an equality check than an inequality one.


That’s all folks!