赞
踩
pow(3,4) # 函数用法:pow(x,y[,z])
- import math
- math.pow(3,4)
- 3**4
比较python在求次方上,使用pow和math.pow以及**带来的差异,会有便在后续使用中能够择优选择。
- 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).
返回值差异
math.pow()返回的值,总是为float,而**和pow(),根据传入的数据类型,返回的值的类型不同,比如pow(2,2)和pow(2,2.0),前者返回的是int型,后者返回的是float型。
上限差异
math.pow()的上线,相比与pow和**两种用法,传入的参数限制得很小。
耗时差异
多次带值测试得如下现象(a,b均为int型):
a = 70 and b = 50 for example
2,各自,保持b不变,a逐渐增大,如下图;
a from 1 to 900 and b keep 50 for example
b form 1 to 100 and a keep 50 for example ,math.pow not happen error
a keep 50 and b change from 1 to 500 and set mathpow =0
math.pow的优缺点是明显的,即,上限小,但运算速度快。
而pow和** 两者,在现测的指标中,表现很相似,有可能底层机制是一样的。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。