Code for calculating execution time using Python

Source: Internet
Author: User
This article mainly introduces the summary of Python execution time calculation methods. For more information, see this article, for more information, see

First of all, let's talk about the pitfalls and problems I encountered in production. I schedule Python script execution and monitor this process. The python script running time is much longer than the program execution time measured by myself in the python script.

It takes about 36 hours to monitor the execution of the python script, and about 4 hours to count the execution time in the python script.

The first thing that comes to mind when a problem occurs in Linux is found to be abnormal.

Then I thought of writing data asynchronously to py2neo in python, blocking process execution.

Finally, the problem is found: the time used by the python script is time. clock (). In this way, the CPU execution time is counted, not the program execution time.

Next, compare the statistical time of several python methods:

Method 1:

import datetimestarttime = datetime.datetime.now()#long running#do something otherendtime = datetime.datetime.now()print (endtime - starttime).seconds

Datetime. datetime. now () gets the current date. after the execution of the program ends, the obtained time value is the execution time of the program.

Method 2:

start = time.time()#long running#do something otherend = time.time()print end-start

Time. time () gets the current time since the epoch (in seconds ). If the system clock provides them, there may be a second score. Therefore, this field returns a floating point type. What we get here is alsoThe execution time of the program.

Method 3:

start = time.clock()#long running#do something otherend = time.clock()print end-start

Time. clock () returns the CPU time since the program starts or is called clock () for the first time. This has the same precision as system records. The return value is also a floating point type. What we get here isCPU execution time.

Note: program execution time = cpu time + io time + sleep or wait time

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

The above is the details of the code using the Python execution time calculation method. For more information, see other related articles in the first PHP community!

Related Article

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.