
% python -m timeit -n 10000 ''ġ0000 loops, best of 3: 365 usec per loop

Besides calling it directly from code, you can also call it from the command-line. Instead of dealing with the different behaviors of time.time() and time.clock() on different platforms, which is often error-prone, Python's timeit module provides a simple way for timing. Most often than not, we should use a wall-clock-based timer to measure a program's performance since it often reflects the productions environment. If the program is expected to run in a system that also runs lots of other programs at the same time, then measuring the program using time.time() makes sense. If the program is expected to run in a system that almost dedicates more than enough resources to the program, i.e., a dedicated web server running a Python-based web application, then measuring the program using time.clock() makes sense since the web application probably will be the major program running on the server. Given the platform-dependent behavior of time.time() and time.clock(), which one should we use to measure the "exact" performance of a program? Well, it depends. Unlike Unix, time.clock() does not return the CPU time, instead it returns the wall-clock time with a higher precision than time.time().
#Timer clock windows#
Running the same program under Windows gives back completely different results:īoth time.time() and time.clock() show that the wall-clock time passed approximately one second. time.clock() has a much higher precision than time.time(). Time.time() shows that the wall-clock time has passed approximately one second while time.clock() shows the CPU time spent on the current process is less than 1 microsecond. Here is an example of running time.time and time.clock on a Unix machine: Another difference between time.time and time.clock is that time.time could return a lower-value than a previous call if the system clock has been set back between the two calls while time.clock always return non-decreasing values. While on Windows, it returns the wall-clock time expressed in seconds elapsed since the first call to this function, based on the Win32 function QueryPerformanceCounter.

On Unix, time.clock returns the current processor time expressed in seconds, i.e., the CPU time it takes to execute the current thread so far. While time.time behaves the same on Unix and on Windows, time.clock has different meanings. time.time is often used to benchmark a program on Windows. For Windows, the epoch is January 1, 1601. For any operatin system, you can always run time.gmtime(0) to find out what epoch is on the given system. time.time returns the time in seconds since the epoch, i.e., the point where the time starts. Two useful functions for time measurement are time.time and time.clock.
Since most of the time functions call platform-specific C library functions with the same name, the semantics of these functions are platform-dependent. Python's time module provides various time-related functions. One should remember that the system clock could be modified by the operating system, thus modifying the system time. System time represents a computer system's notion of the passing of time. Compared to the CPU time, the wall-clock time is often longer because the CPU executing the measured program may also be executing other program's instructions at the same time.Īnother important concept is the so-called system time, which is measured by the system clock.

The wall-clock time is also called elapsed or running time. The second type of time is called wall-clock time, which measures the total time to execute a program in a computer. The first type of time is called CPU or execution time, which measures how much time a CPU spent on executing a program. Last Updated: Wednesday 29 th December 2021Ī prerequisite before we dive into the difference of measuring time in Python is to understand various types of time in the computing world.
