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

后端 未结 16 1454
忘掉有多难
忘掉有多难 2020-11-22 08:22

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          


        
16条回答
  •  不思量自难忘°
    2020-11-22 09:02

    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
    

提交回复
热议问题