程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The fastest way for Python to implement loops (speed comparison between for and while)

編輯:Python

as everyone knows ,Python It's not a more efficient language . In addition, in any language , Loops are very time consuming operations . Suppose any simple one-step operation takes 1 A unit of , Repeat this operation tens of thousands of times , Eventually, the time spent will also increase tens of thousands of times .

while and for yes Python Two commonly used keywords to implement loops , In fact, there is 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 loop\t\t', timeit.timeit(while_loop, number=1))
print('for loop\t\t', timeit.timeit(for_loop, number=1))
if __name__ == '__main__':
main()
# => while loop 4.718853999860585
# => for loop 3.211570399813354

This is a simple summation operation , Calculate from 1 To n The sum of all natural numbers between . You can see for Cycle comparison while Be quick 1.5 second .

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

In each cycle ,while In fact, it's better than for Two more steps : Boundary checks and variables i Self increasing of . That is, each cycle ,while Will do a boundary check (while i < n) And self increasing calculation (i +=1). Both operations are explicit and pure Python Code .

for The loop does not need to perform boundary checking and auto increment operations , No explicit Python Code ( pure Python The code efficiency is lower than the underlying C Code ). When the number of cycles is enough , There is an obvious efficiency gap .

You can add two more functions , stay for Unnecessary boundary checking and self increasing calculation are added to the 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 loop\t\t', timeit.timeit(while_loop, number=1))
print('for loop\t\t', timeit.timeit(for_loop, number=1))
print('for loop with increment\t\t',
timeit.timeit(for_loop_with_inc, number=1))
print('for loop with test\t\t', timeit.timeit(for_loop_with_test, number=1))
if __name__ == '__main__':
main()
# => while loop 4.718853999860585
# => for loop 3.211570399813354
# => for loop with increment 4.602369500091299
# => for loop with test 4.18337869993411

It can be seen that , Increased boundary checking and self augmentation operations do greatly affect for Execution efficiency of the loop .

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

For the above operation of finding the sum of equal difference sequences , With the help of Python Built in sum function , Can get much greater than for or while Execution efficiency of the 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 loop\t\t', timeit.timeit(while_loop, number=1))
print('for loop\t\t', timeit.timeit(for_loop, number=1))
print('sum range\t\t', timeit.timeit(sum_range, number=1))
if __name__ == '__main__':
main()
# => while loop 4.718853999860585
# => for loop 3.211570399813354
# => sum range 0.8658821999561042

You can see , Use built-in functions sum After the substitution cycle , The execution efficiency of code has doubled .

Built in functions sum The accumulation operation of is actually a kind of loop , But it was created by C Language implementation , and for The summation operation in the loop is performed by pure Python Code s += i Realized .C > Python.

Expand your thinking . As a child, I heard that Gauss skillfully calculated 1 To 100 The story of the sum of .1…100 The sum 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 loop\t\t', timeit.timeit(while_loop, number=1))
print('for loop\t\t', timeit.timeit(for_loop, number=1))
print('sum range\t\t', timeit.timeit(sum_range, number=1))
print('math sum\t\t', timeit.timeit(math_sum, number=1))
if __name__ == '__main__':
main()
# => while loop 4.718853999860585
# => for loop 3.211570399813354
# => sum range 0.8658821999561042
# => math sum 2.400018274784088e-06

Final math sum The execution time is about 2.4e-6, A million times shorter . The idea here is , Since the efficiency of the cycle is low , A piece of code needs to be repeated hundreds of millions of times .

Simply don't cycle , Through mathematical formulas , Turn hundreds of millions of cyclic operations into one-step operations . Efficiency has naturally been unprecedentedly strengthened .

The final conclusion ( A little Riddler ):

The fastest way to implement a loop —— —— —— Just don't cycle

about Python for , Use built-in functions whenever possible , Put the pure in the loop Python Code to a minimum .

Of course , Built in functions are not the fastest in some cases . For example, when creating a list , It's the literal way that's faster .

The above is all the content shared this time , Want to know more python Welcome to official account :Python Programming learning circle , send out “J” Free access to , Daily dry goods sharing


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved