Python measures time, use time.time or time.clock

There are multiple times in computing. The first, called CPU time or execution time, measures the time the CPU spends executing a program. The second, called wall clock time, measures the total time spent executing a program. Wall clock time is also known as elapsed time or running time. Compared to CPU time, wall clock time is usually longer because the CPU executing the measured program may be executing instructions from other programs at the same time.

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

Python's time module provides various time-related functions. Since most functions call platform-dependent C library functions of the same name, these functions are platform-dependent.

time.time与time.clock

The two functions used for time measurement are time.time and time.clock. time returns the time since the epoch in seconds. For any operating system you can run time.gmtime(0) to find the epoch for this system. For Unix, the new epoch was January 1, 1970. For Windows, the new era is January 1, 1601. time.time is often used to detect programs on Windows. On Unix systems, time.time does the same thing as Windows, but time.clock has a different meaning. On Unix systems, time.clock returns the current processor time in seconds, i.e. the CPU time spent executing the current thread. On Windows, it returns the elapsed system time in seconds since the function was first called.

Another difference between time.time and time.clock is that if the system clock is set back between calls, time.time may return a small value, while time.clock returns an unincremented value .

Here is an example of running time.time and time.clock on a Unix system:

time.time() shows that the system time has elapsed for about 1 second, while time.clock() shows the CPU time spent on the current process in 1 millisecond. At the same time, you can see that the precision of time.clock() is higher than that of time.time()

Here's how the same program returns different results under Windows:

time.time() and time.clock() show that approximately 1 second has elapsed in the system time. Unlike Unix, time.clock() does not return CPU time, it returns system time with high precision.

time.time() and time.clock() behave differently for different systems, so which one should be used when measuring the exact performance of a program?

It depends on the situation. If the system the program is running on can provide enough resources to the program, for example, a web server running a Python-based web application, it makes more sense to use time.clock() to measure the program, since this web application may be the server on the main program. If the program is running on a system with a large number of other programs running at the same time, measuring with time.time() makes more sense. If this is not the case, a wall-clock-based timer should be used to measure the program's performance, as this usually reflects the program's environment.

timeit module

Dealing with the different behavior of time.time() and time.clock() on different platforms is often error-prone, and Python's timeit module provides an easy way to deal with time. In addition to calling it directly from code, it can also be called from the command line.

E.g:

on Unix-based operating systems

# on Windows

in IDLE

What timing does timeit use? As you can see from the timeit source code, it uses the best possible timer:

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

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

Except in very special cases, the module timeit should generally be used. Another thing to keep in mind is that measuring the performance of a program is a bit more comprehensive, because it is impossible to allocate resources indefinitely to a program while it is being executed, and it is better to measure the average time over many loops than one execution at a time time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325318705&siteId=291194637