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

问题描述 投票:-2回答:1

结果不如预期。 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
python-3.x numbers long-integer pow processing-efficiency
1个回答
0
投票

确实pow()在整数上没有很好的表现(在三种论证形式中)。查看函数的docstring:

码:

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
© www.soinside.com 2019 - 2024. All rights reserved.