|
|
发表于 2015-4-6 16:08:16
|
显示全部楼层
python的底层似乎是用c写的,所以很多的你在使用的功能 是用c实现的,比如这个**操作符,表示的是求幂,第二个函数,你自己用迭代的方法实现了幂函数,执行效率上应该会差很多,- def power_1(x, y):
- return (x ** y)
- def power_2(x, y):
- result = 1
- for i in range(y):
- result *= x
- return result
- import time
- start = time.clock()
- power_1(144764326432, 100)
- time_1 = time.clock()
- power_2(144764326432, 100)
- time_2 = time.clock()
- print("power_1运行了{}秒".format(time_1 - start))
- print("power_2运行了{}秒".format(time_2 - time_1))
复制代码
结果- >>>
- power_1运行了8.659919199746488e-06秒
- power_2运行了4.5224022487565005e-05秒
- >>>
复制代码 |
评分
-
查看全部评分
|