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

后端 未结 16 1449
忘掉有多难
忘掉有多难 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: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...

提交回复
热议问题