python timeit模块的使用(附加append、insert、extend用法)
2017-12-21 10:53
871 查看
测试一段代码的运行时间,在python里面有个很简单的方法,就是使用timeit模块,使用起来超级方便
下面简单介绍一个timeit模块中的函数
主要就是这两个函数:
1, timeit(stmt='pass', setup='pass', timer=<defaulttimer>, number=1000000)
返回:
返回执行stmt这段代码number遍所用的时间,单位为秒,float型
参数:
stmt:要执行的那段代码
setup:执行代码的准备工作,不计入时间,一般是import之类的
timer:这个在win32下是time.clock(),linux下是time.time(),默认的,不用管
number:要执行stmt多少遍
2, repeat(stmt='pass', setup='pass', timer=<defaulttimer>, repeat=3, number=1000000)
这个函数比timeit函数多了一个repeat参数而已,表示重复执行timeit这个过程多少遍,返回一个列表,表示执行每遍的时间
当然,为了方便,python还用了一个Timer类,Timer类里面的函数跟上面介绍的两个函数是一样一样的
class timeit.Timer(stmt='pass', setup='pass',timer=<timer function>)
Timer.timeit(number=1000000)
Timer.repeat(repeat=3,number=1000000)
看懂了吧,一样的,使用的时候哪种方便用哪种 O(∩_∩)O
就相当于
timeit(stmt='pass', setup='pass', timer=<defaulttimer>, number=1000000)
= Timer(stmt='pass', setup='pass', timer=<timerfunction>).timeit(number=1000000)
repeat(stmt='pass', setup='pass', timer=<defaulttimer>, repeat=3, number=1000000)
= Timer(stmt='pass', setup='pass', timer=<timerfunction>).repeat(repeat=3, number=1000000)
下面看个简单的例子吧
import timeit
import math
import pprint
def myfun():
for i in range(100):
for j in range(2, 10):
math.pow(i, 1/j)
n = 100
t1 = timeit.timeit(stmt=myfun, number=n)
pprint.pprint(t1)
t2 = timeit.repeat(stmt=myfun, number=n, repeat=5)
pprint.pprint(t2)
print()
timeitObj = timeit.Timer(stmt=myfun)
t3 = timeitObj.timeit(number=n)
pprint.pprint(t3)
t4 = timeitObj.repeat(number=n, repeat=5)
pprint.pprint(t4)
D:\005-design\001-python\python.exe C:/Users/Administrator/PycharmProjects/untitled/test.py
0.07670407246477397
[0.05263928378686393,
0.0564007225542589,
0.04596403840020144,
0.08785868398187624,
0.0807014070693659]
()
0.04607743527370933
[0.045046244306927,
0.04444133456363253,
0.044189130814860755,
0.05559367055818942,
0.04449898113478035]
Process finished with exit code 0
为了输出美观,使用了pprint这个有意思的模
append 和extend都只需要一个参数,并且自动添加到数组末尾
如果需要添加多个,可用数组嵌套,
但是 append是将嵌套后的数组作为一个对象,
而 extend是将嵌套的数组内容作为多个对象,添加到原数组中
[html] view
plain copy
>>> member = ['1','23','2.1','hehe',[1,2,3]]
>>>
>>> member
['1', '23', '2.1', 'hehe', [1, 2, 3]]
>>> member.append("hello")
>>> member
['1', '23', '2.1', 'hehe', [1, 2, 3], 'hello']
>>> member.extend(['wuha','world'])
>>> member
['1', '23', '2.1', 'hehe', [1, 2, 3], 'hello', 'wuha', 'world']
>>> member.append(['nihao','wohao'])
>>> member
['tiankong', '1', '23', '2.1', 'hehe', [1, 2, 3], 'hello', 'wuha', 'world', ['nihao', 'wohao']]
>>>
insert 两个参数,指定插入的位置
[html] view
plain copy
>>> member.insert(0,'tiankong')
>>> member
['tiankong', '1', '23', '2.1', 'hehe', [1, 2, 3], 'hello', 'wuha', 'world']
下面简单介绍一个timeit模块中的函数
主要就是这两个函数:
1, timeit(stmt='pass', setup='pass', timer=<defaulttimer>, number=1000000)
返回:
返回执行stmt这段代码number遍所用的时间,单位为秒,float型
参数:
stmt:要执行的那段代码
setup:执行代码的准备工作,不计入时间,一般是import之类的
timer:这个在win32下是time.clock(),linux下是time.time(),默认的,不用管
number:要执行stmt多少遍
2, repeat(stmt='pass', setup='pass', timer=<defaulttimer>, repeat=3, number=1000000)
这个函数比timeit函数多了一个repeat参数而已,表示重复执行timeit这个过程多少遍,返回一个列表,表示执行每遍的时间
当然,为了方便,python还用了一个Timer类,Timer类里面的函数跟上面介绍的两个函数是一样一样的
class timeit.Timer(stmt='pass', setup='pass',timer=<timer function>)
Timer.timeit(number=1000000)
Timer.repeat(repeat=3,number=1000000)
看懂了吧,一样的,使用的时候哪种方便用哪种 O(∩_∩)O
就相当于
timeit(stmt='pass', setup='pass', timer=<defaulttimer>, number=1000000)
= Timer(stmt='pass', setup='pass', timer=<timerfunction>).timeit(number=1000000)
repeat(stmt='pass', setup='pass', timer=<defaulttimer>, repeat=3, number=1000000)
= Timer(stmt='pass', setup='pass', timer=<timerfunction>).repeat(repeat=3, number=1000000)
下面看个简单的例子吧
import timeit
import math
import pprint
def myfun():
for i in range(100):
for j in range(2, 10):
math.pow(i, 1/j)
n = 100
t1 = timeit.timeit(stmt=myfun, number=n)
pprint.pprint(t1)
t2 = timeit.repeat(stmt=myfun, number=n, repeat=5)
pprint.pprint(t2)
print()
timeitObj = timeit.Timer(stmt=myfun)
t3 = timeitObj.timeit(number=n)
pprint.pprint(t3)
t4 = timeitObj.repeat(number=n, repeat=5)
pprint.pprint(t4)
D:\005-design\001-python\python.exe C:/Users/Administrator/PycharmProjects/untitled/test.py
0.07670407246477397
[0.05263928378686393,
0.0564007225542589,
0.04596403840020144,
0.08785868398187624,
0.0807014070693659]
()
0.04607743527370933
[0.045046244306927,
0.04444133456363253,
0.044189130814860755,
0.05559367055818942,
0.04449898113478035]
Process finished with exit code 0
为了输出美观,使用了pprint这个有意思的模
append 和extend都只需要一个参数,并且自动添加到数组末尾
如果需要添加多个,可用数组嵌套,
但是 append是将嵌套后的数组作为一个对象,
而 extend是将嵌套的数组内容作为多个对象,添加到原数组中
[html] view
plain copy
>>> member = ['1','23','2.1','hehe',[1,2,3]]
>>>
>>> member
['1', '23', '2.1', 'hehe', [1, 2, 3]]
>>> member.append("hello")
>>> member
['1', '23', '2.1', 'hehe', [1, 2, 3], 'hello']
>>> member.extend(['wuha','world'])
>>> member
['1', '23', '2.1', 'hehe', [1, 2, 3], 'hello', 'wuha', 'world']
>>> member.append(['nihao','wohao'])
>>> member
['tiankong', '1', '23', '2.1', 'hehe', [1, 2, 3], 'hello', 'wuha', 'world', ['nihao', 'wohao']]
>>>
insert 两个参数,指定插入的位置
[html] view
plain copy
>>> member.insert(0,'tiankong')
>>> member
['tiankong', '1', '23', '2.1', 'hehe', [1, 2, 3], 'hello', 'wuha', 'world']
相关文章推荐
- python中计时工具timeit模块的基本用法 分类: python python基础学习 2013-08-08 10:05 2072人阅读 评论(0) 收藏
- python中计时工具timeit模块的基本用法
- python中timeit模块用法
- python中计时工具timeit模块的基本用法
- 使用Python的timeit模块
- python中计时工具timeit模块的基本用法
- 如何使用python timeit模块使用实践
- python中计时工具timeit模块的基本用法
- python中计时工具timeit模块的基本用法
- python中计时工具timeit模块的基本用法
- python中计时工具timeit模块的基本用法
- Python中append和extend的用法
- Python and time模块使用
- python list中append()与extend()用法
- python time模块用法实例详解
- python time模块用法实例详解
- python中append()和extend()的使用说明
- python的time模块使用
- Python中time,datetime模块的常见使用方法
- python的time模块使用