Home  >  Article  >  Backend Development  >  Which method of Python loops the fastest may subvert your understanding!

Which method of Python loops the fastest may subvert your understanding!

王林
王林forward
2023-04-17 23:10:011247browse

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.

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 the 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 variable i. That is, every time a loop is executed, while will perform a boundary check (while i < n) and an increment calculation (i =1). Both steps are explicit pure Python code.

The 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 to add unnecessary boundary checks and 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 check and auto-increment operation have indeed greatly affected 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.

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 performed by pure Python code s = i realized. C > Python.

Expand your thinking. 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 a loop - - — ——Just don’t use loops

For Python, use built-in functions as much as possible to minimize the pure Python code in the loop.

The above is the detailed content of Which method of Python loops the fastest may subvert your understanding!. 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