Python method for calculating program runtime based on time module

Source: Internet
Author: User
This article mainly introduces Python based on the time module to find the program runtime of the method, involving the use of the Python timing module and numerical operations related operation skills, the need for friends can refer to the next

The example in this paper describes how Python is based on the time module to find the program runtime. Share to everyone for your reference, as follows:

This timestamp is easy to log when the program's run time can take advantage of the number of milliseconds in the Unix system, from 1970.1.1 to the present.

The method is the beginning of the program to save a variable once, after the end of the program is taken to save a variable, and the start of the program to subtract the time stamp can be obtained.

The way to take this timestamp in Python is to use it when the time class is introduced time.time(); . That's in Java System.currentTimeMillis() .

As Python asks for the exact time of the current few months and days, it needs to be like Java, which can refer to Python's method of producing a unique number using the current time and random number.

The method is as follows, taking a 100,000,000, 100 million cycles of time as an example


Import Time;time_start=time.time (); #time. Time () is the number of milliseconds from 1970.1.1 to the current I=0;while i<100000000:i+=1time_end=time.time (); #time. Time () The number of milliseconds from 1970.1.1 to the current print Time_end-time_start,print "s"

The results of the operation are as follows:

The result of Time_end-time_start subtraction is a fraction of a second unit.
So the final output is then mended with a unit, s, seconds.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.