#计时工具
import time, sys, math
def funcSqrt(x): return math.sqrt(x)
def funcStars(x): return x ** 0.5
def funcPow(x): return pow(x, 0.5)
#重复100000次的总时间
def mytime(func, num=10000, re=100000):
if sys.platform =='win32':
start = time.clock()
else:
start = time.time()
for i in range(re):
res = func(num)
thetime = time.clock() - start
return thetime, res
#重复50次,取每1000次用时最小的值
def mybest(func, times=50):
best = 2 * 32
for i in range(times):
time, res = mytime(func,re=1000)
if best > time:
best = time
return best, res
print(sys.version)
for fun in (mytime, mybest):
print('=' * 40)
print('<%s>' % fun.__name__)
for func in (funcSqrt, funcStars, funcPow):
print('-' * 40)
print('%-10s>>> %.5f >>> %f' % (func.__name__, fun(func)[0], fun(func)[-1]))
第四章的习题
最新推荐文章于 2023-03-31 20:20:32 发布