Python 3.x Python3中pow(x,y,z)和x**y%z速度的模糊性。哪一个是有效的?

Python 3.x Python3中pow(x,y,z)和x**y%z速度的模糊性。哪一个是有效的?,python-3.x,numbers,long-integer,pow,processing-efficiency,Python 3.x,Numbers,Long Integer,Pow,Processing Efficiency,结果并不像预期的那样。pow(x,y,z)必须是有效的,但是结果会改变。为什么? import timeit print(timeit.timeit("pow(2505626,1520321,2700643)")) output:3.700144177302718 print(timeit.timeit("pow(2505626,1520321,2700643)",number=1000000)) output:4.591832527890801 print(timeit.timeit("250

结果并不像预期的那样。pow(x,y,z)必须是有效的,但是结果会改变。为什么?

import timeit
print(timeit.timeit("pow(2505626,1520321,2700643)"))
output:3.700144177302718
print(timeit.timeit("pow(2505626,1520321,2700643)",number=1000000))
output:4.591832527890801
print(timeit.timeit("2505626**1520321%2700643",number=1000000))
output:0.014752348884940147

事实上,
pow()
在整数上没有很好的性能(以三参数形式)。请参见函数的docstring:

代码:


不要问代码的图像。将代码复制到问题本身。您仅在一次执行中使用timeit,并且希望得到准确的结果?@ArtOfCode image将准确显示输出。@alfasin我已经做了数百次了。向我解释一下,为什么效率会改变?你可以用图片来支持你的文章,但是所有相关的代码和输出都必须直接复制到你的文章中。
def pow(*args, **kwargs): # real signature unknown
    """
    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.
    """
    pass