Home  >  Article  >  Backend Development  >  Which Python looping method is the fastest?

Which Python looping method is the fastest?

WBOY
WBOYforward
2023-04-14 11:07:021298browse

As we all know, Python is not an efficient language. In addition, looping is a very time-consuming operation in any language. If any simple single-step operation takes 1 unit of time, if this operation is repeated tens of thousands of times, the final time spent will also increase tens of thousands of times.

Which Python looping method is the fastest?

while and for are two keywords commonly used to implement loops in Python. There is actually a gap in their operating efficiency. For example, the following test code:​

import timeit


def while_loop(n=100_000_000):
i = 0
s = 0
while i < n:
s += i
i += 1
return s


def for_loop(n=100_000_000):
s = 0
for i in range(n):
s += i
return s


def main():
print('while looptt', timeit.timeit(while_loop, number=1))
print('for looptt', timeit.timeit(for_loop, number=1))


if __name__ == '__main__':
main()
# => while loop 4.718853999860585
# => for loop 3.211570399813354

This is a simple sum operation that calculates the sum of all natural numbers from 1 to n. You can see that the for loop is 1.5 seconds faster than while.

The main difference lies in the different mechanisms between the two.

In each loop, while​ actually performs two more steps than for​: bounds checking and incrementing the variable i​. That is, every time a loop is performed, while will do a boundary check (while i

for​ loop does not need to perform bounds checking and increment operations, and does not add explicit Python code (pure Python code is less efficient than the underlying C code). When the number of cycles is large enough, a significant efficiency gap appears.

You can add two more functions, and add unnecessary boundary checks and auto-increment calculations in the for loop:

import timeit


def while_loop(n=100_000_000):
i = 0
s = 0
while i < n:
s += i
i += 1
return s


def for_loop(n=100_000_000):
s = 0
for i in range(n):
s += i
return s


def for_loop_with_inc(n=100_000_000):
s = 0
for i in range(n):
s += i
i += 1
return s


def for_loop_with_test(n=100_000_000):
s = 0
for i in range(n):
if i < n:
pass
s += i
return s


def main():
print('while looptt', timeit.timeit(while_loop, number=1))
print('for looptt', timeit.timeit(for_loop, number=1))
print('for loop with incrementtt',
timeit.timeit(for_loop_with_inc, number=1))
print('for loop with testtt', timeit.timeit(for_loop_with_test, number=1))


if __name__ == '__main__':
main()
# => while loop 4.718853999860585
# => for loop 3.211570399813354
# => for loop with increment4.602369500091299
# => for loop with test 4.18337869993411

It can be seen that the added boundary checks and auto-increment operations are indeed greatly Affects the execution efficiency of the for loop.

As mentioned earlier, Python’s underlying interpreter and built-in functions are implemented in C language. The execution efficiency of C language is much greater than that of Python.

For the above operation of finding the sum of arithmetic sequences, with the help of Python’s built-in sum function, the execution efficiency can be much greater than that of a for or while loop.

import timeit


def while_loop(n=100_000_000):
i = 0
s = 0
while i < n:
s += i
i += 1
return s


def for_loop(n=100_000_000):
s = 0
for i in range(n):
s += i
return s


def sum_range(n=100_000_000):
return sum(range(n))


def main():
print('while looptt', timeit.timeit(while_loop, number=1))
print('for looptt', timeit.timeit(for_loop, number=1))
print('sum rangett', timeit.timeit(sum_range, number=1))


if __name__ == '__main__':
main()
# => while loop 4.718853999860585
# => for loop 3.211570399813354
# => sum range0.8658821999561042

It can be seen that after using the built-in function sum​ to replace the loop, the execution efficiency of the code has doubled.

The accumulation operation of the built-in function sum is actually a loop, but it is implemented in C language, while the sum operation in the for loop is implemented by the pure Python code s = i. C>Python.

Expand your thinking again. We've all grown up hearing stories about childhood Gauss's ingenious calculation of sums from 1 to 100. The sum of 1…100 is equal to (1 100) * 50. This calculation method can also be applied to the above summation operation.

import timeit


def while_loop(n=100_000_000):
i = 0
s = 0
while i < n:
s += i
i += 1
return s


def for_loop(n=100_000_000):
s = 0
for i in range(n):
s += i
return s


def sum_range(n=100_000_000):
return sum(range(n))


def math_sum(n=100_000_000):
return (n * (n - 1)) // 2


def main():
print('while looptt', timeit.timeit(while_loop, number=1))
print('for looptt', timeit.timeit(for_loop, number=1))
print('sum rangett', timeit.timeit(sum_range, number=1))
print('math sumtt', timeit.timeit(math_sum, number=1))


if __name__ == '__main__':
main()
# => while loop 4.718853999860585
# => for loop 3.211570399813354
# => sum range0.8658821999561042
# => math sum 2.400018274784088e-06

The final execution time of math sum is about 2.4e-6, which is shortened by millions of times. The idea here is that since the efficiency of loops is low, a piece of code has to be executed hundreds of millions of times.

Simply don’t loop, and use mathematical formulas to turn hundreds of millions of loop operations into only one step. Efficiency has naturally been enhanced as never before.

Final conclusion (a bit Riddler):

The fastest way to implement loops——————is not to loop

For Python, try your best to Use built-in functions to minimize pure Python code in loops.

The above is the detailed content of Which Python looping method is the fastest?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete