Three method codes using Python execution time calculation

Source: Internet
Author: User
This article mainly introduces the Python execution time calculation method Summary of the relevant information, the need for friends can refer to the following

First of all, I encountered the pit, production problems encountered, I dispatched the Python script to execute and monitor the process, Python script run time is much larger than the Python script's own statistics of the program execution time.

The time to monitor Python script execution is 36 hours, while the Python script counts the time it takes to execute in 4 hours or so.

The first thing that comes to mind when the problem leaks is that Linux is out of the question, looking for various logs that don't find anything unusual.

Then I think of the Py2neo written data used in Python to asynchronous, blocking the process execution.

Finally, we finally find the problem: The Python script uses statistical time as Time.clock (), which counts the CPU execution time, not the execution time of the program.

Next, compare the statistical time patterns of several python:

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, the time value obtained by this method is the time the program executes.

Method 2:

Start = Time.time () #long running#do something otherend = Time.time () print End-start

Time.time () Gets the current time (in seconds) since the era. If the system clock provides them, there may be fractions of seconds. So this place returns a floating-point type. The execution time of the program is also obtained here .

Method 3:

Start = Time.clock () #long running#do something otherend = Time.clock () print End-start

Time.clock () returns the CPU time since the start of the program or the first time the clock () was called. This has as much precision as the system record. The return is also a floating-point type. The CPU execution time is obtained here.

Note: Program execution time =CPU time + IO time + hibernate or wait time

Thank you for reading, hope to help everyone, thank you for the support of this site!

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.