pow(x,y,z) 中的歧义和 python 中的 x**y%z 速度 3. 哪个有效?

Ambiguity in pow(x,y,z) and x**y%z speed in python 3. Which one is efficient?

结果与预期不符。 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() 在整数(三参数形式)上表现不佳。查看函数的文档字符串:

代码:

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