[英]Will a+=1 be faster than a = a+1 in Python?
I'm not sure whether it's the same in Python. 我不确定在Python中是否相同。
Has anyone tried that before? 有人尝试过吗?
http://docs.python.org/library/operator#operator.iadd http://docs.python.org/library/operator#operator.iadd
There hardly is a difference in the work python performs for either statement: python对任一语句执行的工作几乎没有区别:
>>> import dis
>>> def inplace_add():
... a = 0
... a += 1
...
>>> def add_and_assign():
... a = 0
... a = a + 1
...
>>> dis.dis(inplace_add)
2 0 LOAD_CONST 1 (0)
3 STORE_FAST 0 (a)
3 6 LOAD_FAST 0 (a)
9 LOAD_CONST 2 (1)
12 INPLACE_ADD
13 STORE_FAST 0 (a)
16 LOAD_CONST 0 (None)
19 RETURN_VALUE
>>> dis.dis(add_and_assign)
2 0 LOAD_CONST 1 (0)
3 STORE_FAST 0 (a)
3 6 LOAD_FAST 0 (a)
9 LOAD_CONST 2 (1)
12 BINARY_ADD
13 STORE_FAST 0 (a)
16 LOAD_CONST 0 (None)
19 RETURN_VALUE
The difference is a INPLACE_ADD
versus a BINARY_ADD
. 区别在于
INPLACE_ADD
与BINARY_ADD
。
The resulting timings are too close to call which one would be faster: 产生的计时太接近了,无法调用哪个会更快:
>>> import timeit
>>> timeit.timeit('inplace_add', 'from __main__ import inplace_add', number=10000000)
0.32667088508605957
>>> timeit.timeit('add_and_assign', 'from __main__ import add_and_assign', number=10000000)
0.34172606468200684
So, in python, the difference is negligible. 因此,在python中,差异可以忽略不计。 Don't worry about it.
不用担心
Nope 不
>>> bar = timeit.Timer("a += 1", "a = 0")
>>> bar.timeit(number=1000000)
0.064391136169433594
>>> bar = timeit.Timer("a = a + 1", "a = 0")
>>> bar.timeit(number=1000000)
0.064393997192382812
>>>
Yep, but the difference is marginal. 是的,但是差别很小。
>>> timeit.Timer('x += 1', 'x = 0').timeit(10**8)
5.7387330532073975
>>> timeit.Timer('x = x + 1', 'x = 0').timeit(10**8)
6.04801607131958
>>> timeit.Timer('x += 1', 'x = 0').timeit(10**8)
5.790481090545654
>>> timeit.Timer('x = x + 1', 'x = 0').timeit(10**8)
6.083467960357666
I took a slightly different approach using the cProfile
module: 我使用
cProfile
模块采取了稍微不同的方法:
$ python -m cProfile test.py
4 function calls in 0.397 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.397 0.397 test.py:2(<module>)
1 0.205 0.205 0.205 0.205 test.py:2(add1)
1 0.192 0.192 0.192 0.192 test.py:6(add2)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
aaron@zebrafish:~/pyad$ cat test.py
def add1(a):
for x in xrange(10 ** 6):
a += 1
def add2(a):
for x in xrange(10 ** 6):
a = a + 1
add1(0)
add2(0)
After about 20 runs I would conclude that add2 (using a = a + 1
) was very slightly faster, but not in all cases (perhaps try it with a greater number of loops). 经过大约20次运行后,我得出的结论是add2(使用
a = a + 1
)稍微快一点,但并非在所有情况下都可以(也许尝试使用更多的循环)。 This is probably not the best heuristic, but I figure a greater number of repetitions with larger and larger numbers should indicate a performance difference. 这可能不是最好的启发式方法,但是我认为重复次数越多,次数越多,说明性能会有差异。
EDIT - results for 10 ** 9 calls: 编辑-10 ** 9次通话的结果:
1 216.119 216.119 216.119 216.119 test.py:2(add1)
1 195.364 195.364 195.364 195.364 test.py:6(add2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.