为什么 Python 中 range()循环比 while 循环快?

有一天,我在做一些 Python 基准测试,我发现了一些有趣的东西。下面是两个循环,它们或多或少做着相同的事情。循环1的执行时间大约是循环2的两倍。

循环1:

int i = 0
while i < 100000000:
i += 1

循环2:

for n in range(0,100000000):
pass

为什么第一个循环慢得多?我知道这只是个小例子,但它引起了我的兴趣。Range ()函数有什么特别之处使它比以同样的方式递增变量更有效吗?

74862 次浏览

Because you are running more often in code written in C in the interpretor. i.e. i+=1 is in Python, so slow (comparatively), whereas range(0,...) is one C call the for loop will execute mostly in C too.

range() is implemented in C, whereas i += 1 is interpreted.

Using xrange() could make it even faster for large numbers. Starting with Python 3.0 range() is the same as previously xrange().

see the disassembly of python byte code, you may get a more concrete idea

use while loop:

1           0 LOAD_CONST               0 (0)
3 STORE_NAME               0 (i)


2           6 SETUP_LOOP              28 (to 37)
>>    9 LOAD_NAME                0 (i)              # <-
12 LOAD_CONST               1 (100000000)      # <-
15 COMPARE_OP               0 (<)              # <-
18 JUMP_IF_FALSE           14 (to 35)          # <-
21 POP_TOP                                     # <-


3          22 LOAD_NAME                0 (i)              # <-
25 LOAD_CONST               2 (1)              # <-
28 INPLACE_ADD                                 # <-
29 STORE_NAME               0 (i)              # <-
32 JUMP_ABSOLUTE            9                  # <-
>>   35 POP_TOP
36 POP_BLOCK

The loop body has 10 op

use range:

1           0 SETUP_LOOP              23 (to 26)
3 LOAD_NAME                0 (range)
6 LOAD_CONST               0 (0)
9 LOAD_CONST               1 (100000000)
12 CALL_FUNCTION            2
15 GET_ITER
>>   16 FOR_ITER                 6 (to 25)        # <-
19 STORE_NAME               1 (n)            # <-


2          22 JUMP_ABSOLUTE           16                # <-
>>   25 POP_BLOCK
>>   26 LOAD_CONST               2 (None)
29 RETURN_VALUE

The loop body has 3 op

The time to run C code is much shorter than intepretor and can be ignored.

Most of Python's built in method calls are run as C code. Code that has to be interpreted is much slower. In terms of memory efficiency and execution speed the difference is gigantic. The python internals have been optimized to the extreme, and it's best to take advantage of those optimizations.

It must be said that there is a lot of object creation and destruction going on with the while loop.

i += 1

is the same as:

i = i + 1

But because Python ints are immutable, it doesn't modify the existing object; rather it creates a brand new object with a new value. It's basically:

i = new int(i + 1)   # Using C++ or Java-ish syntax

The garbage collector will also have a large amount of cleanup to do. "Object creation is expensive".

I think the answer here is a little more subtle than the other answers suggest, though the gist of it is correct: the for loop is faster because more of the operations happen in C and less in Python.

More specifically, in the for loop case, two things happen in C that in the while loop are handled in Python:

  1. In the while loop, the comparison i < 100000000 is executed in Python, whereas in the for loop, the job is passed to the iterator of range(100000000), which internally does the iteration (and hence bounds check) in C.

  2. In the while loop, the loop update i += 1 happens in Python, whereas in the for loop again the iterator of range(100000000), written in C, does the i+=1 (or ++i).

We can see that it is a combination of both of these things that makes the for loop faster by manually adding them back to see the difference.

import timeit


N = 100000000




def while_loop():
i = 0
while i < N:
i += 1




def for_loop_pure():
for i in range(N):
pass




def for_loop_with_increment():
for i in range(N):
i += 1




def for_loop_with_test():
for i in range(N):
if i < N: pass




def for_loop_with_increment_and_test():
for i in range(N):
if i < N: pass
i += 1




def main():
print('while loop\t\t', timeit.timeit(while_loop, number=1))
print('for pure\t\t', timeit.timeit(for_loop_pure, number=1))
print('for inc\t\t\t', timeit.timeit(for_loop_with_increment, number=1))
print('for test\t\t', timeit.timeit(for_loop_with_test, number=1))
print('for inc+test\t', timeit.timeit(for_loop_with_increment_and_test, number=1))




if __name__ == '__main__':
main()

I tried this both with the number 100000000 a literal constant and with it being a variable N as would be more typical.

# inline constant N
while loop      3.5131139
for pure        1.3211338000000001
for inc         3.5477727000000003
for test        2.5209639
for inc+test    4.697028999999999


# variable N
while loop      4.1298240999999996
for pure        1.3526357999999998
for inc         3.6060175
for test        3.1093069
for inc+test    5.4753364

As you can see, in both cases, the while time is very close to the difference of for inc+test and for pure. Note also that in the case where we use the N variable, the while has an additional slowdown to repeatedly lookup the value of N, but the for does not.

It's really crazy that such trivial modifications can result in over 3x code speedup, but that's Python for you. And don't even get me started on when you can use a builtin over a loop at all....