" POW" (电)功能 [英] "pow" (power) function
问题描述
我对那里的数字计算器有几个问题:
是否pow(x,2)简单的方形x,或者它首先计算对数
(如果指数不是整数则必要)?
是否x ** 0.5"使用与sqrt(x)相同的算法,还是使用一些
其他(可能效率较低)基于对数的算法?
谢谢,
Russ
Russ写道:我有一个数字计算器的几个问题:
是否pow(x,2)简单的方形x,或者它首先计算对数
(如果指数不是整数则必要)?
x ** 0.5是否为x ** 0.5。使用与sqrt(x)相同的算法,还是使用一些基于对数的其他(可能效率较低)算法?
可以尝试和timeit
111 ** 111
10736201288847422580121456504669550195985072399422 48048047759111756250761957833470224912261700936346 21466103743092986967777786330067310159463303558666 91009102601778558729553962214205731543706973022937 5357546494103400699864397711L timeit.Timer( pow(111,111))。timeit()
40.888447046279907 timeit.Timer(" 111 ** 111")。timeit()
39.732122898101807 timeit.Timer(" 111 ** 0.5" ;)。timeit()
2.0990891456604004 timeit.Timer(" pow(111,0.5)"。)timeit()
4.1776390075683594 timeit.Timer(" 111 ** 0.3")。timeit ()
2.3824679851531982 timeit.Timer(" pow(111,0.3)")。timeit()
4.29450416 56494141
有趣的结果
似乎**计算速度更快
我没有shure哪个算法,但我假设所有的Python都是,
来调用底层C pow()函数。
Sam
SchüleDaniel< uv ** @ rz.uni-karlsruhe.de>写道:>>> timeit.Timer(" 111 ** 0.3")。timeit()2.3824679851531982>>> timeit.Timer(" pow(111,0.3)"。)timeit()
4.2945041656494141
有趣的结果
似乎**计算得更快
也许是111 ** 0.3如果timeit使用eval,则解析速度比pow(111,0.3)快。
此外,pow()可能会产生更多的子程序调用开销 - 更好地检查
两者的字节码版本。
I have a couple of questions for the number crunchers out there:
Does "pow(x,2)" simply square x, or does it first compute logarithms
(as would be necessary if the exponent were not an integer)?
Does "x**0.5" use the same algorithm as "sqrt(x)", or does it use some
other (perhaps less efficient) algorithm based on logarithms?
Thanks,
Russ
Russ wrote:I have a couple of questions for the number crunchers out there:
Does "pow(x,2)" simply square x, or does it first compute logarithms
(as would be necessary if the exponent were not an integer)?
Does "x**0.5" use the same algorithm as "sqrt(x)", or does it use some
other (perhaps less efficient) algorithm based on logarithms?
you can try and timeit
111**111 10736201288847422580121456504669550195985072399422 48048047759111756250761957833470224912261700936346 21466103743092986967777786330067310159463303558666 91009102601778558729553962214205731543706973022937 5357546494103400699864397711L timeit.Timer("pow(111,111)").timeit() 40.888447046279907 timeit.Timer("111**111").timeit() 39.732122898101807 timeit.Timer("111**0.5").timeit() 2.0990891456604004 timeit.Timer("pow(111,0.5)").timeit() 4.1776390075683594 timeit.Timer("111**0.3").timeit() 2.3824679851531982 timeit.Timer("pow(111,0.3)").timeit()
4.2945041656494141
interesting result
seems that ** computates faster
I not shure which algorithm,but I am assumeing that all Python does,is
to call the underlying C pow() function.
Sam
Schüle Daniel <uv**@rz.uni-karlsruhe.de> writes:>>> timeit.Timer("111**0.3").timeit() 2.3824679851531982 >>> timeit.Timer("pow(111,0.3)").timeit()
4.2945041656494141
interesting result
seems that ** computates faster
Maybe "111**0.3" parses faster than pow(111,0.3), if timeit uses eval.
Also, pow() may incur more subroutine call overhead--better check
the bytecode for both versions.
这篇关于" POW" (电)功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!