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

后端 未结 16 1452
忘掉有多难
忘掉有多难 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 08:58

    As of 3.3, time.clock() is deprecated, and it's suggested to use time.process_time() or time.perf_counter() instead.

    Previously in 2.7, according to the time module docs:

    time.clock()

    On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms.

    On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.

    Additionally, there is the timeit module for benchmarking code snippets.

    0 讨论(0)
  • 2020-11-22 09:01

    I use this code to compare 2 methods .My OS is windows 8 , processor core i5 , RAM 4GB

    import time
    
    def t_time():
        start=time.time()
        time.sleep(0.1)
        return (time.time()-start)
    
    
    def t_clock():
        start=time.clock()
        time.sleep(0.1)
        return (time.clock()-start)
    
    
    
    
    counter_time=0
    counter_clock=0
    
    for i in range(1,100):
        counter_time += t_time()
    
        for i in range(1,100):
            counter_clock += t_clock()
    
    print "time() =",counter_time/100
    print "clock() =",counter_clock/100
    

    output:

    time() = 0.0993799996376
    
    clock() = 0.0993572257367
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 09:03

    To the best of my understanding, time.clock() has as much precision as your system will allow it.

    0 讨论(0)
  • 2020-11-22 09:06

    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.

    0 讨论(0)
  • 2020-11-22 09:06

    Right answer : They're both the same length of a fraction.

    But which faster if subject is time ?

    A little test case :

    import timeit
    import time
    
    clock_list = []
    time_list = []
    
    test1 = """
    def test(v=time.clock()):
        s = time.clock() - v
    """
    
    test2 = """
    def test(v=time.time()):
        s = time.time() - v
    """
    def test_it(Range) :
        for i in range(Range) :
            clk = timeit.timeit(test1, number=10000)
            clock_list.append(clk)
            tml = timeit.timeit(test2, number=10000)
            time_list.append(tml)
    
    test_it(100)
    
    print "Clock Min: %f Max: %f Average: %f" %(min(clock_list), max(clock_list), sum(clock_list)/float(len(clock_list)))
    print "Time  Min: %f Max: %f Average: %f" %(min(time_list), max(time_list), sum(time_list)/float(len(time_list)))
    

    I am not work an Swiss labs but I've tested..

    Based of this question : time.clock() is better than time.time()

    Edit : time.clock() is internal counter so can't use outside, got limitations max 32BIT FLOAT, can't continued counting if not store first/last values. Can't merge another one counter...

    0 讨论(0)
提交回复
热议问题