定时python程序的CPU时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15176619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Timing the CPU time of a python program?
提问by Nathan Bush
I would like to time a snippet of my code, and I would like just the CPU execution time (ignoring operating system processes etc).
我想为我的代码片段计时,我只想 CPU 执行时间(忽略操作系统进程等)。
I've tried time.clock(), it appears too imprecise, and gives a different answer each time. (In theory surely if I run it again for the same code snippet it should return the same value??)
我试过 time.clock(),它看起来太不精确了,每次都给出不同的答案。(理论上,如果我再次为相同的代码片段运行它,它应该返回相同的值??)
I've played with timeit for about an hour. Essentially what kills it for me is the "setup" process, I end up having to import around 20 functions which is impractical as I'm practically just re-writing my code into the setup section to try and use it.
我玩了 timeit 大约一个小时。对我来说,基本上杀死它的是“设置”过程,我最终不得不导入大约 20 个函数,这是不切实际的,因为我实际上只是将我的代码重新编写到设置部分以尝试使用它。
Cprofiles are looking more and more attractive, but do they return CPU time? also, a minor point - it outputs way too much information. Is there any way to get the outputted information into a txt or .dat file so I can actually read it?
Cprofiles 看起来越来越有吸引力,但它们是否返回 CPU 时间?还有一个小问题——它输出了太多的信息。有什么方法可以将输出的信息放入 txt 或 .dat 文件中,以便我可以实际阅读它?
Cheers
干杯
OS: Ubuntu Program: python 2.7
操作系统:Ubuntu 程序:python 2.7
回答by Shedokan
It sounds like you are looking for a way to time the process-wideexecution 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-widetime, meaning that other proceses can interfere with your measurments.
听起来您正在寻找一种方法来计时进程范围内的执行时间,您可以做的最好的事情是使用timeit.default_timer()它提供最精确的 time.clock() 或 time.time() 函数当前平台,但它是系统范围的时间,这意味着其他过程可能会干扰您的测量。
Here's the info from the docs of timeit.default_timer():
以下是 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.
以特定于平台的方式定义默认计时器。在 Windows 上,time.clock() 的粒度为微秒,但 time.time() 的粒度为 1/60 秒。在 Unix 上,time.clock() 具有 1/100 秒的粒度,而 time.time() 精确得多。在任一平台上,default_timer() 测量挂钟时间,而不是 CPU 时间。这意味着在同一台计算机上运行的其他进程可能会干扰计时。
You should try testing c-modules which might have access to different timing apis.
您应该尝试测试可能可以访问不同计时 api 的 c 模块。
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:
最好的方法是使用仅在python 3.3 及更高版本中可用的time.process_time(),这是文档中的信息:
Return the value (in fractional seconds) of the sum of the system and user CPU timeof the current process. It does not include time elapsed during sleep. It is process-wideby definition. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.
返回当前进程的系统和用户 CPU 时间总和的值(以秒为单位)。它不包括睡眠期间经过的时间。根据定义,它是流程范围的。返回值的参考点未定义,因此只有连续调用结果之间的差异才有效。
回答by Martijn Pieters
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.
而不是time.clock(),使用timeit.default_timer(); 它为您的平台使用最准确的选项。例如,对于 Ubuntu,这将time.time()改为使用。
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.
使用时timeit,创建一个设置函数,然后您可以将其重新用于timeit。是的,这看起来有点工作,但它可以确保您对真正想要测量的时间进行计时,而不是在时间关键的测量部分中包含设置代码。

