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

2018-12-31 10:43发布

Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?

for example:

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

vs.

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

标签: python time
17条回答
几人难应
2楼-- · 2018-12-31 10:50

Use the time.time() is preferred.

查看更多
大哥的爱人
3楼-- · 2018-12-31 10:51

Short answer: use time.clock() for timing in Python.

On *nix systems, clock() returns the processor time as a floating point number, expressed in seconds. On Windows, it returns the seconds elapsed since the first call to this function, as a floating point number.

time() returns the the seconds since the epoch, in UTC, as a floating point number. There is no guarantee that you will get a better precision that 1 second (even though time() returns a floating point number). Also note that if the system clock has been set back between two calls to this function, the second function call will return a lower value.

查看更多
高级女魔头
4楼-- · 2018-12-31 10:54

clock() -> floating point number

Return the CPU time or real time since the start of the process or since the first call to clock(). This has as much precision as the system records.

time() -> floating point number

Return the current time in seconds since the Epoch. Fractions of a second may be present if the system clock provides them.

Usually time() is more precise, because operating systems do not store the process running time with the precision they store the system time (ie, actual time)

查看更多
宁负流年不负卿
5楼-- · 2018-12-31 10:56

Comparing test result between Ubuntu Linux and Windows 7.

On Ubuntu

>>> start = time.time(); time.sleep(0.5); (time.time() - start)
0.5005500316619873

On Windows 7

>>> start = time.time(); time.sleep(0.5); (time.time() - start)
0.5
查看更多
浪荡孟婆
6楼-- · 2018-12-31 10:58

To extend on @Hill's results, here's a test using python 3.4.3 on Xubuntu 16.04 through wine:
(timeit.default_timer will use time.clock() because it sees the OS as 'win32')

when using Windows, clock() is usually more accurate, but this is not the case on wine...
you can see here time() is more accurate than clock(), which is usually the case with Linux and Mac.

查看更多
初与友歌
7楼-- · 2018-12-31 10:59

For my own practice. time() has better precision than clock() on Linux. clock() only has precision less than 10 ms. While time() gives prefect precision. My test is on CentOS 6.4, python 2.6

using time():

1 requests, response time: 14.1749382019 ms
2 requests, response time: 8.01301002502 ms
3 requests, response time: 8.01491737366 ms
4 requests, response time: 8.41021537781 ms
5 requests, response time: 8.38804244995 ms

using clock():

1 requests, response time: 10.0 ms
2 requests, response time: 0.0 ms 
3 requests, response time: 0.0 ms
4 requests, response time: 10.0 ms
5 requests, response time: 0.0 ms 
6 requests, response time: 0.0 ms
7 requests, response time: 0.0 ms 
8 requests, response time: 0.0 ms
查看更多
登录 后发表回答