Python's time.clock() vs. time.time() accuracy?

Which is far better to make use of for timing in Python? time.clock() or time.time()? Which one gives even more precision?

for example:

start = time.clock()
... do something
elapsed = (time.clock() - start)

vs.

start = time.time()
... do something
elapsed = (time.time() - start)
451
2022-06-30 01:05:37
Source Share
Answers: 9

To the most effective of my understanding, time.clock() has as much accuracy as your system will certainly permit it.

3
2022-06-30 02:17:42
Source

Depends on what you care about. If you mean WALL TIME (as in, the time on the clock on your wall), time.clock() provides NO accuracy because it may manage CPU time.

20
2022-06-30 01:16:44
Source

Short solution: make use of time.clock() for timing in Python.

On * nix systems, clock() returns the cpu time as a floating factor number, shared in secs. On Windows, it returns the secs expired given that the first phone call to this function, as a floating factor number.

time() returns the the secs given that the date, in UTC, as a floating factor number. There is no warranty that you will certainly get a far better accuracy that 1 2nd (despite the fact that time() returns a floating factor number). Additionally note that if the system clock has actually been set back in between 2 phone call to this function, the 2nd function call will certainly return a lower value.

2
2022-06-30 01:13:00
Source

On Unix time.clock() gauges the quantity of CPU time that has actually been made use of by the existing procedure, so it is no excellent for gauging elapsed time from some factor in the past. On Windows it will certainly gauge wall surface - clock secs expired given that the first phone call to the function. On either system time.time() will certainly return secs passed given that the date.

If you are creating code that is suggested just for Windows, either will certainly function (though you'll make use of both in different ways - no reduction is essential for time.clock()). If this is mosting likely to work on a Unix system or you desire code that is assured to be mobile, you will certainly intend to make use of time.time().

4
2022-06-30 01:12:38
Source

The distinction is really system - details.

clock() is really various on Windows than on Linux, as an example.

For the type of instances you define, you possibly desire the "timeit" component rather.

7
2022-06-30 01:11:31
Source

As of 3.3, time.clock(), and also it is recommended to make use of time.process_time() or time.perf_counter() rather.

Formerly in 2.7, according to the time module docs :

time.clock()

On Unix, return the existing cpu time as a floating factor number shared in secs. The accuracy, and also actually the really definition of the definition of “processor time", relies on that of the C function of the very same name, yet regardless, this is the function to make use of for benchmarking Python or timing formulas.

On Windows, this function returns wall surface - clock secs expired given that the first phone call to this function, as a floating factor number, based upon the Win32 function QueryPerformanceCounter(). The resolution is commonly far better than one split second.

In addition, there is the timeit component for benchmarking code fragments.

180
2022-06-30 01:10:54
Source

The short answer is: most of the time time.clock() will be better. However, if you're timing some hardware (for example some algorithm you put in the GPU), then time.clock() will get rid of this time and time.time() is the only solution left.

Note: whatever the method used, the timing will depend on factors you cannot control (when will the process switch, how often, ...), this is worse with time.time() but exists also with time.clock(), so you should never run one timing test only, but always run a series of test and look at mean/variance of the times.

52
2022-06-30 01:10:37
Source

Others have actually addressed re: time.time() vs, time.clock()

However, if you are timing the implementation of a block of code for benchmarking/profiling objectives, you need to have a look at the timeit.

27
2022-06-30 01:10:34
Source

clock() - > floating point number

Return the CPU time or live given that the start of the procedure or given that the first phone call to clock(). This has as much precision as the system documents.

time() - > floating point number

Return the existing time in secs given that the Epoch. Split seconds might exist if the system clock gives them.

Generally time() is extra specific, due to the fact that running systems do not store the procedure running time with the precision they store the system time (ie, real time)

21
2022-06-30 01:10:32
Source