Home  >  Article  >  Backend Development  >  Python method to find program running time based on time module

Python method to find program running time based on time module

巴扎黑
巴扎黑Original
2017-09-21 11:03:221778browse

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 Method to find program running time based on 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 finds the precise time of the current few months and days, it needs to involve this constant like Java. You can refer to "Python's method of using the current time and random numbers to generate a unique number".

The specific method is as follows, taking the time consumption of a 100,000,000, 100 million cycle 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 time_end-time_start subtraction is directly a decimal in seconds.
So the final output is supplemented with another unit, s, seconds.

The above is the detailed content of Python method to find program running time based on time module. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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