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

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

    As others have noted time.clock() is deprecated in favour of time.perf_counter() or time.process_time(), but Python 3.7 introduces nanosecond resolution timing with time.perf_counter_ns(), time.process_time_ns(), and time.time_ns(), along with 3 other functions.

    These 6 new nansecond resolution functions are detailed in PEP 564:

    time.clock_gettime_ns(clock_id)

    time.clock_settime_ns(clock_id, time:int)

    time.monotonic_ns()

    time.perf_counter_ns()

    time.process_time_ns()

    time.time_ns()

    These functions are similar to the version without the _ns suffix, but return a number of nanoseconds as a Python int.

    As others have also noted, use the timeit module to time functions and small code snippets.

提交回复
热议问题