Timing the CPU time of a python program?

后端 未结 2 818
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 16:45

I would like to time a snippet of my code, and I would like just the CPU execution time (ignoring operating system processes etc).

I\'ve tried time.clock(), it appea

相关标签:
2条回答
  • 2020-12-06 17:21

    It sounds like you are looking for a way to time the process-wide execution time, the best thing you can do is use timeit.default_timer() which offers the most precise time.clock() or time.time() function available on the current platform, but it is system-wide time, meaning that other proceses can interfere with your measurments.

    Here's the info from the docs of timeit.default_timer():

    Define a default timer, in a platform-specific manner. On Windows, time.clock() has microsecond granularity, but time.time()‘s granularity is 1/60th of a second. On Unix, time.clock() has 1/100th of a second granularity, and time.time() is much more precise. On either platform, default_timer() measures wall clock time, not the CPU time. This means that other processes running on the same computer may interfere with the timing.

    You should try testing c-modules which might have access to different timing apis.


    The best possible way to do this is by using time.process_time() which is only available in python 3.3 and up, here's the info from the docs:

    Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep. It is process-wide by definition. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.

    0 讨论(0)
  • 2020-12-06 17:30

    Instead of time.clock(), use timeit.default_timer(); it uses the most accurate option for your platform. For Ubuntu, for example, this will use time.time() instead.

    When using timeit, create one setup function that you then can re-use for timeit. Yes, this looks like a bit of work but it ensures that you time what you really wanted to measure, and not include setup code in the time-critical measured section.

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