This article mainly introduces Python's method of finding program running time based on the time module, involving the use of the Python time module and related numerical operation skills. Friends in need can refer to it
The examples in this article describe Python A method to find the program running time based on the time module. Share it with everyone for your reference, the details are as follows:
To record the running time of the program, you can use the number of milliseconds from 1970.1.1 to the present time in the Unix system. This timestamp can be easily completed.
The method is to take it once and store it into a variable at the beginning of the program. After the program ends, it takes it once and stores it into a variable. It can be obtained by subtracting the timestamp from the beginning of the program.
The way to get this timestamp in Python is to introduce the time class and use time.time();
to get it out. That is System.currentTimeMillis()
in Java.
When Python seeks the precise time of the current few months and days, it needs to involve this constant like Java.
The specific method is as follows, with a 100,000,000, 100 million times loop Time-consuming as an example
import time; time_start=time.time();#time.time()为1970.1.1到当前时间的毫秒数 i=0; while i<100000000: i+=1 time_end=time.time();#time.time()为1970.1.1到当前时间的毫秒数 print time_end-time_start, print "s"
The running results are as follows:
The result of subtracting time_end-time_start is directly A fraction of a second.
So the final output is supplemented with another unit, s, seconds.
The above is the detailed content of Example sharing of methods for finding program running time using the time module in Python. For more information, please follow other related articles on the PHP Chinese website!