Fab*_*ano 4 python python-2.7 python-3.x
在Python中,如果内置pow()函数与3个参数一起使用,则最后一个用作取幂的模数,从而产生模幂运算操作.
换句话说,pow(x, y, z)相当于(x ** y) % z,但相应的Python帮助,pow()可能更有效.
当我计算两个版本时,我得到了相反的结果,pow()版本似乎比等效语法慢:
Python 2.7:
>>> import sys
>>> print sys.version
2.7.11 (default, May 2 2016, 12:45:05)
[GCC 4.9.3]
>>>
>>> help(pow)
Help on built-in function pow in module __builtin__: <F2> Show Source
pow(...)
pow(x, y[, z]) -> number
With two arguments, equivalent to x**y. With three arguments,
equivalent to (x**y) % z, but may be more efficient (e.g. for longs).
>>>
>>> import timeit
>>> st_expmod = '( 65537 ** 767587 ) % 14971787'
>>> st_pow = 'pow(65537, 767587, 14971787)'
>>>
>>> timeit.timeit(st_expmod)
0.016651153564453125
>>> timeit.timeit(st_expmod)
0.016621112823486328
>>> timeit.timeit(st_expmod)
0.016611099243164062
>>>
>>> timeit.timeit(st_pow)
0.8393168449401855
>>> timeit.timeit(st_pow)
0.8449611663818359
>>> timeit.timeit(st_pow)
0.8767969608306885
>>>
Run Code Online (Sandbox Code Playgroud)
Python 3.4:
>>> import sys
>>> print(sys.version)
3.4.3 (default, May 2 2016, 12:47:35)
[GCC 4.9.3]
>>>
>>> help(pow)
Help on built-in function pow in module builtins:
pow(...)
pow(x, y[, z]) -> number
With two arguments, equivalent to x**y. With three arguments,
equivalent to (x**y) % z, but may be more efficient (e.g. for ints).
>>>
>>> import timeit
>>> st_expmod = '( 65537 ** 767587 ) % 14971787'
>>> st_pow = 'pow(65537, 767587, 14971787)'
>>>
>>> timeit.timeit(st_expmod)
0.014722830994287506
>>> timeit.timeit(st_expmod)
0.01443593599833548
>>> timeit.timeit(st_expmod)
0.01485627400688827
>>>
>>> timeit.timeit(st_pow)
3.3412855619972106
>>> timeit.timeit(st_pow)
3.2800855879904702
>>> timeit.timeit(st_pow)
3.323372773011215
>>>
Run Code Online (Sandbox Code Playgroud)
Python 3.5:
>>> import sys
>>> print(sys.version)
3.5.1 (default, May 2 2016, 14:34:13)
[GCC 4.9.3
>>>
>>> help(pow)
Help on built-in function pow in module builtins:
pow(x, y, z=None, /)
Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
Some types, such as ints, are able to use a more efficient algorithm when
invoked using the three argument form.
>>>
>>> import timeit
>>> st_expmod = '( 65537 ** 767587 ) % 14971787'
>>> st_pow = 'pow(65537, 767587, 14971787)'
>>>
>>> timeit.timeit(st_expmod)
0.014827249979134649
>>> timeit.timeit(st_expmod)
0.014763347018742934
>>> timeit.timeit(st_expmod)
0.014756042015505955
>>>
>>> timeit.timeit(st_pow)
3.6817933860002086
>>> timeit.timeit(st_pow)
3.6238356370013207
>>> timeit.timeit(st_pow)
3.7061628740048036
>>>
Run Code Online (Sandbox Code Playgroud)
以上数字的解释是什么?
编辑:
在答案之后我看到在st_expmod版本中,计算没有在运行时执行,而是由解析器和表达式变成常量..
使用Python2中@ user2357112建议的修复:
>>> timeit.timeit('(a**b) % c', setup='a=65537; b=767587; c=14971787', number=150)
370.9698350429535
>>> timeit.timeit('pow(a, b, c)', setup='a=65537; b=767587; c=14971787', number=150)
0.00013303756713867188
Run Code Online (Sandbox Code Playgroud)
你实际上**并没有使用和计算计算%,因为结果由字节码编译器进行常量折叠.避免:
timeit.timeit('(a**b) % c', setup='a=65537; b=767587; c=14971787')
Run Code Online (Sandbox Code Playgroud)
而pow版本将赢得胜利.