This article mainly introduces python related information on several methods of recording program running time. Friends who need it can refer to
The first one I saw handwritten, similar to the following Type:
import datetime def time_1(): begin = datetime.datetime.now() sum = 0 for i in xrange(10000000): sum = sum + i end = datetime.datetime.now() return end-begin print time_1()
The output is as follows:
➜ Python python time_1.py 0:00:00.280797
Three methods of python recording program running time
Python is provided here Three methods of recording program running time, with implementation code and final comparison. For your reference:
Method 1
import datetime starttime = datetime.datetime.now() #long running endtime = datetime.datetime.now() print (endtime - starttime).seconds
Method 2
start = time.time() run_fun() end = time.time() print end-start
Method 3
start = time.clock() run_fun() end = time.clock() print end-start
Both methods 1 and 2 include the time that other programs use the CPU, which is the program The running time from start to end of program.
Method 3 only calculates the CPU time of program running
The above is the detailed content of Introduction to the method of recording program running time in Python. For more information, please follow other related articles on the PHP Chinese website!