标签:调用 class port imei highlight tostring value lis ejs
timeit模块:
timeit 模块定义了接受两个参数的 Timer 类。两个参数都是字符串。 第一个参数是你要计时的语句或者函数。 传递给 Timer 的第二个参数是为第一个参数语句构建环境的导入语句。 从内部讲, timeit 构建起一个独立的虚拟环境, 手工地执行建立语句,然后手工地编译和执行被计时语句。
一旦有了 Timer 对象,最简单的事就是调用 timeit(),它接受一个参数为每个测试中调用被计时语句的次数,默认为一百万次;返回所耗费的秒数。
测试一个列表推导式与for循环的时间
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import timeit foooo = """ sum = [] for i in range(1000): sum.append(i) """ print (timeit.timeit(stmt = "[i for i in range(1000)]" , number = 100000 )) print (timeit.timeit(stmt = foooo, number = 100000 )) #res: #3.2855970134734345 #8.19918414604134 |
使用列表推导式要比正常使用list追加元素 通过10w次循环会快上近5秒左右,速度快上近三倍。
timeit 模块抽象出了;两个可以直接使用的方法,包了一层可以让我们不用关心内部的实现,下面看一下模块里面的代码:
1
2
3
4
5
6
7
8
9
|
def timeit(stmt = "pass" , setup = "pass" , timer = default_timer, number = default_number): """Convenience function to create Timer object and call timeit method.""" return Timer(stmt, setup, timer).timeit(number) def repeat(stmt = "pass" , setup = "pass" , timer = default_timer, repeat = default_repeat, number = default_number): """Convenience function to create Timer object and call repeat method.""" return Timer(stmt, setup, timer).repeat(repeat, number) |
可以看到这两个方法都是对Timer类包了一层这几个参数:
stmt: 这个参数就是statement,可以把要进行计算时间的代码放在里面。他可以直接接受字符串的表达式,也可以接受单个变量,也可以接受函数。
setup: 这个参数可以将stmt的环境传进去。比如各种import和参数什么的。
timer: 这个参数一般使用不到,具体使用可以参看文档。
Timer类下面还有repeat和timeit方法 使用也非常方便就是 timeit.timeit 和 timeit.repeat。
一个就是上面例子的timeit,一个就是repeat 其实repeat就比timeit多了一个执行Timer次数的参数。这个执行次数会以数组的形式返回.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import timeit foooo = """ sum = [] for i in range(1000): sum.append(i) """ print (timeit.repeat(stmt = "[i for i in range(1000)]" , repeat = 2 , number = 100000 )) print ( min (timeit.repeat(stmt = "[i for i in range(1000)]" , repeat = 2 , number = 100000 ))) #res: #[3.4540683642063277, 3.300991128415932] #3.321008256502136 |
我们可以根据此,对所有执行时间取min最小值,平均值,最大值得到我们想要的数据。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# coding: utf-8 import timeit # 初始化类 x = """ say_hi.ParseFromString(p) """ y = """ simplejson.loads(x) """ print (timeit.timeit(stmt = x, setup = "import say_hi_pb2;" "say_hi = say_hi_pb2.SayHi();" "say_hi.id = 13423;" "say_hi.something = ‘axiba‘;" "say_hi.extra_info = ‘xiba‘;" "p =say_hi.SerializeToString()" , number = 1000000 )) print (timeit.timeit(stmt = y, setup = "import simplejson; " "json={" "‘id‘: 13423," "‘something‘: ‘axiba‘," "‘extra_info‘: ‘xiba‘," "};" "x = simplejson.dumps(json)" , number = 1000000 ))<br> |
另外需要补充一点是,如果你想直接 stmt 那里执行函数。可以把函数申明在当前文件中,然后在 stmt = ‘func()’ 执行函数。然后使用 setup = ‘from __main__ import func’ 即可,如果要import 多个需要使用 setup = from __main__ import func; import simplejson‘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
def test1(): n = 0 for i in range ( 101 ): n + = i return n def test2(): return sum ( range ( 101 )) def test3(): return sum (x for x in range ( 101 )) if __name__ = = ‘__main__‘ : from timeit import Timer t1 = Timer( "test1()" , "from __main__ import test1" ) t2 = Timer( "test2()" , "from __main__ import test2" ) t3 = Timer( "test3()" , "from __main__ import test3" ) print (t1.timeit( 10000 )) print (t2.timeit( 10000 )) print (t3.timeit( 10000 )) print (t1.repeat( 3 , 10000 )) print (t2.repeat( 3 , 10000 )) print (t3.repeat( 3 , 10000 )) t4 = timeit.timeit(stmt = test1,setup = "from __main__ import test1" ,number = 10000 ) t5 = timeit.timeit(stmt = test2,setup = "from __main__ import test2" ,number = 10000 ) t6 = timeit.timeit(stmt = test3,setup = "from __main__ import test3" ,number = 10000 ) print (t4) #0.05130029071325269 print (t5) #0.015494466822610305 print (t6) #0.05650903115721077 print (timeit.repeat(stmt = test1,setup = "from __main__ import test1" ,number = 10000 )) # [0.05308853391023148, 0.04544335904366706, 0.05969025402337652] print (timeit.repeat(stmt = test2,setup = "from __main__ import test2" ,number = 10000 )) # [0.012824560678924846, 0.017111019558035345, 0.01429126826003152] print (timeit.repeat(stmt = test3,setup = "from __main__ import test3" ,number = 10000 )) # [0.07385010910706968, 0.06244617606430164, 0.06273494371932059] #res: 0.043916918200588385 0.014892355541932578 0.05214884436618059 [ 0.04372713709398021 , 0.04197132052492908 , 0.04255431716177577 ] [ 0.014356804181737959 , 0.012456603785177323 , 0.012629659578433372 ] [ 0.0543709217115389 , 0.05334180294099272 , 0.05334931226535494 ] |
标签:调用 class port imei highlight tostring value lis ejs
原文地址:https://www.cnblogs.com/valorchang/p/11282126.html