Python measurement time, with Time.time or Time.clock

Source: Internet
Author: User

There are many times in the computer field. The first is called CPU time or execution time, which is used to measure the amount of time the CPU spends executing a program. The second is called the Wall clock time, which measures the total time when a program is executed. The wall clock time is also known as elapsed time or elapsed time. The wall clock time is usually longer than the CPU time, because the program that the CPU performs the measurement may also be executing instructions from other programs.

Another important concept is the so-called system time, measured by the system clock. System time represents the concept of time transfer in computer systems. Remember that the system clock can be modified by the operating system, which is to modify the system time.

The Python time module provides a variety of temporal-related functions. Because most functions call platform-related C library functions with the same name, these functions are platform-dependent.

Time.time and Time.clock

The two functions for time measurement are time.time and Time.clock. Time is returned in seconds since the new era. For any operating system, you can run Time.gmtime (0) to find a new era for this system. For UNIX, the new era is January 1, 1970. For Windows, the new era is January 1, 1601. Time.time is typically used to detect programs on Windows. On UNIX systems, Time.time is the same as windows, but Time.clock has different meanings. On UNIX systems, Time.clock returns the current processor time in seconds, for example, the CPU time spent executing the current thread. On Windows, it is the system time in seconds that elapsed since the function was first called.

Another difference between Time.time and Time.clock is that if the system clock is recalled between two calls, the Time.time may return a smaller value, and the value returned by Time.clock does not grow.

The following are examples of running time.time and Time.clock on UNIX systems:

Time.time () shows that the system time has elapsed for about 1 seconds, while Time.clock () shows that the CPU time spent on the current process is 1 milliseconds. You can also see that the accuracy of time.clock () is higher than time.time ()

The following are the same programs that return different results under Windows:

Time.time () and Time.clock () show that the system time has roughly elapsed 1 seconds Kushiro. Unlike Unix, Time.clock () does not return CPU time, it returns the system time, and the precision is high.

Time.time () and Time.clock () have different behaviors for different systems, so which one should be used when measuring the accuracy of the program?

This is subject to availability. If a program is running a system that provides sufficient resources to a program, such as a Web server that runs a Python-based web application, it makes more sense to use Time.clock () to measure the program, because the Web application may be the primary program on the server. If the program is running on a system that also runs a large number of other programs, it makes sense to use Time.time () for measurements. If this is not the case, you should use a wall clock-based timer to measure the performance of the program, as it usually reacts to the environment of the program.

Timeit Module

Handling the different behaviors of time.time () and Time.clock () on different platforms is often error-prone, and the Python Timeit module provides a simple way to process time. In addition to being called directly from your code, you can also invoke it from the command line.

For example:

On UNIX-based operating systems

#在Windows上

In the Idle

What timings does the Timeit use? As you can see from the Timeit source code, it uses the best timers:

Timeit Another important mechanism is that it disables garbage collection during execution, as follows:

If garbage collection is enabled, the performance of the measurement program is more accurate, for example, when a program allocates resources and frees resources for a large number of objects, it should be enabled during its setup:

In addition to very special cases, the module Timeit should generally be used. It is also important to remember that measuring the performance of a program is considered comprehensive because it is not possible to allocate resources indefinitely when executing a program, and measuring average time in many loops is better than measuring one time in a single execution.

Python measurement time, with Time.time or Time.clock

Related Article

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.