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

查看:22
本文介绍了定时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.clock() 或 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天全站免登陆