Home >Backend Development >Python Tutorial >python runtime method
This article mainly introduces several methods of Python running time in detail, and analyzes the pros and cons of each running time method. Interested friends can refer to
The earliest I have seen handwriting , similar to the following:
import datetime def time_1(): begin = datetime.datetime.now() sum = 0 for i in xrange(10000000): sum = sum + i end = datetime.datetime.now() return end-begin print time_1()
The output is as follows:
➜ Python python time_1.py
0:00:00.280797
Another method is to use the timeit module. The usage method is as follows:
In [5]: import timeit In [6]: timeit.timeit("sum(range(100))") Out[6]: 1.2272648811340332
You can also use this timeit module on the command line, as follows:
➜ 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
Note:The timeit module will run the program multiple times to obtain a more accurate time, so the impact of repeated execution needs to be avoided. For example, for operations such as x.sort(), since after the first execution, the subsequent operations are already sorted, the accuracy is affected.
Another method is to use the cProfile module. The code is as follows and the name is time_1.py:
import datetime def time_1(): begin = datetime.datetime.now() sum = 0 for i in xrange(10000000): sum = sum + i end = datetime.datetime.now() return end-begin if __name__ == '__main__': print time_1() import cProfile cProfile.run('time_1()')
The result of running the program is as follows :
➜ 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}
The last line in the code at the beginning is cProfile.run('main()'), it prompts that there is no main(), replace main() Just change it to the function name.
The above is the entire content of this article. I hope it will be helpful to everyone learning python programming.
For more articles related to python running time methods, please pay attention to the PHP Chinese website!