Measuring elapsed time with the Time module

前端 未结 10 839
执念已碎
执念已碎 2020-12-02 03:13

With the Time module in python is it possible to measure elapsed time? If so, how do I do that?

I need to do this so that if the cursor has been in a widget for a c

10条回答
  •  -上瘾入骨i
    2020-12-02 03:55

    In programming, there are 2 main ways to measure time, with different results:

    >>> print(time.process_time()); time.sleep(10); print(time.process_time())
    0.11751394000000001
    0.11764988400000001  # took  0 seconds and a bit
    >>> print(time.perf_counter()); time.sleep(10); print(time.perf_counter())
    3972.465770326
    3982.468109075       # took 10 seconds and a bit
    
    • Processor Time: This is how long this specific process spends actively being executed on the CPU. Sleep, waiting for a web request, or time when only other processes are executed will not contribute to this.

      • Use time.process_time()
    • Wall-Clock Time: This refers to how much time has passed "on a clock hanging on the wall", i.e. outside real time.

      • Use time.perf_counter()

        • time.time() also measures wall-clock time but can be reset, so you could go back in time
        • time.monotonic() cannot be reset (monotonic = only goes forward) but has lower precision than time.perf_counter()

提交回复
热议问题