Several methods for analyzing the python program running time, python program running

Source: Internet
Author: User

Several methods for analyzing the python program running time, python program running

I have seen handwriting at the earliest, similar to the following:

  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()

The output is as follows:

➜  Python python time_1.py0:00:00.280797

 

Another method is to use the timeit module. The method is as follows:

In [5]: import timeitIn [6]: timeit.timeit("sum(range(100))")Out[6]: 1.2272648811340332

You can also use the timeit module on the command line as follows:

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

Note: The timeit module runs the program multiple times to get a more precise time, so you need to avoid the impact of repeated execution. For example, an operation like x. sort (), because after the first execution, the operation is already sorted, and the accuracy is affected.

 

Another method is to use the cProfile module. The Code is as follows and the name is time_1.py:

  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()')

The running result is as follows:

➜  Python python time_1.py0: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.py0: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 () '), prompting that no main () exists. Change main () to the function name.

 

Here is the simplest application. For details, refer to the document or directly help (xxx)

 

 

 

  

 

 

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.