Several computational methods of Python execution time __python

Source: Internet
Author: User

First of all, I encountered the pit, the production of problems encountered, I dispatched the Python script to execute and monitor this process, the Python script run much longer than the Python script in its own statistical execution time.
The time to monitor the execution of a Python script is 36 hours , while the Python script counts itself for about 4 hours .
The first thing that comes to mind after a problem is that Linux is out of the question, looking for a variety of logs that do not find anything unusual.
Then I think of the Py2neo write data that is used in Python to block the execution of the process.
Finally, the problem is found: the way Python scripts use statistical time is Time.clock (), which counts the CPU execution time, not the execution time of the program.
Next, compare the statistical times of several Python ways: Method 1:

Import datetime
StartTime = Datetime.datetime.now ()
#long running
#do something other
endtime = Datetime.datetime.now ()
print (endtime-starttime). seconds

Datetime.datetime.now () Gets the current date, which, after the execution of the program, obtains the time value of the program execution . Method 2:

Start = Time.time ()
#long running
#do something other end
= 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. This also gets the execution time of the program . Method 3:

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

Time.clock () returns the CPU time since the start of the program or the first time the clock () is invoked. 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

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.