Home >Backend Development >Python Tutorial >How to measure script running time in Python
The way python scripts use statistical time is time.clock()
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()获取的是当前日期,在程序执行结束之后,这个方式获得的时间值为程序执行的时间。
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 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
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!