How to measure script running time in Python

anonymity
Release: 2019-05-25 13:43:35
Original
4217 people have browsed it

The way python scripts use statistical time is time.clock()

How to measure script running time in Python

Next, let’s compare several python statistical time methods:

Method 1:

import datetime
starttime = datetime.datetime.now()
#long running
#do something other
endtime = datetime.datetime.now()
print (endtime - starttime).seconds
datetime.datetime.now()获取的是当前日期,在程序执行结束之后,这个方式获得的时间值为程序执行的时间。
Copy after login

Method 2:

start = time.time()
#long running
#do something other
end = time.time()
print end-start
Copy after login

time.time() Gets the current time in seconds since the epoch. Fractions of seconds may be present if the system clock provides them. So what this place returns is a floating point type. What is obtained here is also the execution time of the program.

Method 3:

start = time.clock()
#long running
#do something other
end = time.clock()
print end-start
Copy after login

time.clock() returns the CPU time since the start of the program or the first time clock() was called. This has as much precision as the system records. What is returned is also a floating point type. What is obtained here is the execution time of the CPU.

Note: Program execution time = cpu time io time sleep or waiting time

The above is the detailed content of How to measure script running time in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template