分析python程序运行时间的几种方法

最早见过手写的,类似于下面这种:

1
2
3
4
5
6
7
8
9
10
11
1 import datetime
 2
 3 def time_1():
 4     begin = datetime.datetime.now()
 5     sum = 0
 6     for i in xrange(10000000):
 7         sum = sum + i
 8     end = datetime.datetime.now()
 9     return end-begin
10
11 print time_1()

输出如下:

1
2
➜  Python python time_1.py
0:00:00.280797

 

另外一种方法是使用timeit模块,使用方法如下:

1
2
3
4
In [5]: import timeit
 
In [6]: timeit.timeit("sum(range(100))")
Out[6]: 1.2272648811340332

还可以在命令行上使用这种timeit模块,如下:

1
2
3
4
➜  Python python -m timeit -s"import time_1 as t" "t.time_1()"
0:00:00.282044
 
10 loops, best of 3: 279 msec per loop

注意:timeit模块会多次运行程序以获得更精确的时间,所以需要避免重复执行带来的影响。比方说x.sort()这种操作,因为第一次执行之后,后边已经是排好的了,准确性就收到了影响。

 

还有一种方法是使用cProfile模块,代码如下,名字为time_1.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1 import datetime
 2
 3 def time_1():
 4     begin = datetime.datetime.now()
 5     sum = 0
 6     for i in xrange(10000000):
 7         sum = sum + i
 8     end = datetime.datetime.now()
 9     return end-begin
10
11 if __name__ == '__main__':
12     print time_1()
13
14 import cProfile
15 cProfile.run('time_1()')

运行程序结果如下:  

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
➜  Python python time_1.py
0:00:00.282828
         2 function calls in 0.000 seconds
 
   Ordered by: standard name
 
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
 
 
Traceback (most recent call last):
  File "time_1.py", line 15, in <module>
    cProfile.run('main()')
  File "/usr/lib/python2.7/cProfile.py", line 29, in run
    prof = prof.run(statement)
  File "/usr/lib/python2.7/cProfile.py", line 135, in run
    return self.runctx(cmd, dict, dict)
  File "/usr/lib/python2.7/cProfile.py", line 140, in runctx
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
NameError: name 'main' is not defined
➜  Python vi time_1.py
➜  Python python time_1.py
0:00:00.284642
         5 function calls in 0.281 seconds
 
   Ordered by: standard name
 
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.281    0.281 <string>:1(<module>)
        1    0.281    0.281    0.281    0.281 time_1.py:3(time_1)
        2    0.000    0.000    0.000    0.000 {built-in method now}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

一开始代码里最后一行写的是cProfile.run('main()'),提示没有main(),将main()改成函数名字就可以了

 

这里是最简单的应用,具体大家可以去看看文档,或者直接help(xxx)  

 

 

Python入门推荐这本书,

 Python编程从入门到实践 (可点击直达京东)

 

posted @   wswang  阅读(54497)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示