time.time vs. timeit.timeit

后端 未结 2 1314
温柔的废话
温柔的废话 2020-11-30 00:58

Sometimes, I like to time how long it takes parts of my code to run. I\'ve checked a lot of online sites and have seen, at large, two main ways to do this. One is using

2条回答
  •  情书的邮戳
    2020-11-30 01:56

    timeit is more accurate, for three reasons:

    • it repeats the tests many times to eliminate the influence of other tasks on your machine, such as disk flushing and OS scheduling.
    • it disables the garbage collector to prevent that process from skewing the results by scheduling a collection run at an inopportune moment.
    • it picks the most accurate timer for your OS, time.time or time.clock in Python 2 and time.perf_counter() on Python 3. See timeit.default_timer.

提交回复
热议问题