计时python程序的CPU时间? [英] Timing the CPU time of a python program?

查看:768
本文介绍了计时python程序的CPU时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想给我的代码片段计时,我只想CPU执行时间(忽略操作系统进程等).

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

我尝试过time.clock(),它看起来太不精确了,每次都给出不同的答案. (理论上,如果我再次针对相同的代码段再次运行它,它应该返回相同的值?)

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??)

我已经用timeit玩了大约一个小时.从本质上讲,杀死我的是设置"过程,最终我不得不导入大约20个函数,这是不切实际的,因为我实际上只是将我的代码重新写入设置部分以尝试使用它.

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.

Cprofiles看起来越来越有吸引力,但是它们会返回CPU时间吗?还有一个小问题-它输出太多信息.有什么方法可以将输出的信息转换为txt或.dat文件,以便我可以实际读取它?

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?

欢呼

操作系统:Ubuntu程序:python 2.7

OS: Ubuntu Program: python 2.7

推荐答案

听起来您正在寻找计时整个过程的时间的方法,最好的方法是使用 timeit.default_timer()可提供最精确的时间. ()或time.time()函数在当前平台上可用,但是它是系统范围的时间,这意味着其他过程可能会干扰您的测量.

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.

以下是来自timeit.default_timer()文档的信息:

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

以特定于平台的方式定义默认计时器.在Windows上, time.clock()具有微秒级的粒度,但是time.time()的 粒度为1/60秒.在Unix上,time.clock()具有1/100 秒粒度,并且time.time()更精确.在 无论使用哪种平台, default_timer()都会测量挂钟时间,而不是CPU 时间.这意味着同一台计算机上运行的其他进程可能 干扰时间.

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.

您应该尝试测试可能可以访问不同计时API的c模块.

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

执行此操作的最佳方法是使用 time.process_time( )(仅在 python 3.3及更高版本中可用),以下是来自文档的信息:

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:

返回系统的总和的值(以秒为单位),并 当前进程的用户CPU时间.不包括经过的时间 在睡眠中.根据定义,它是整个流程.参考点 返回值是不确定的,因此只有 连续通话的结果是有效的.

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.

这篇关于计时python程序的CPU时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆