Timing the CPU time of a python program?

后端 未结 2 837
伪装坚强ぢ
伪装坚强ぢ 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: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.

提交回复
热议问题