如何使用资源模块来衡量一个函数的运行时间? [英] How to use the resource module to measure the running time of a function?

查看:39
本文介绍了如何使用资源模块来衡量一个函数的运行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Python代码测量函数的CPU运行时间和挂钟运行时间.

I want to measure the CPU running time and wall clock running time of functions using Python code.

此处建议使用资源模块:如何分别测量函数的 CPU 运行时间和挂钟运行时间,作为 Python 代码(不是来自终端)?

The resource module was suggested here: How to measure CPU running time and wall clock running time of a function, separately, as Python code (not from terminal)?

这里是模块文档:http://docs.python.org/2/library/resource.html

问题在于:

1) 我不知道如何用它来衡量一个函数的运行时间.

1) I can't figure out how to use it to measure the running time of a function.

2) 我不知道如何从返回的对象中提取该信息.

2) I don't know how to extract that information from the object returned.

我该怎么做?

推荐答案

只需调用 getrusage 执行函数前后,减去你关心的字段,就大功告成了.由于 resource 不做挂墙时间,因此您需要为此使用单独的函数.

Just call getrusage before and after executing the function, subtract the fields you care about, and you're done. Since resource doesn't do wall time, you'll need to use a separate function for that.

你可以把它包装在一个辅助函数中,甚至是一个装饰器,就像这样:

You can wrap that up in a helper function, or even a decorator, like this:

import datetime
import functools
import resource
import sys

def timed(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        r0 = resource.getrusage(resource.RUSAGE_SELF)
        t0 = datetime.datetime.now()
        retval = func(*args, **kwargs)
        r = resource.getrusage(resource.RUSAGE_SELF)
        t = datetime.datetime.now()
        sys.stderr.write('{}: utime {} stime {} wall: {}\n'.format(
            func.__name__,
            datetime.timedelta(seconds=r.ru_utime-r0.ru_utime),
            datetime.timedelta(seconds=r.ru_stime-r0.ru_stime),
            t-t0))
        return retval
    return wrapper

@timed
def myfunc(i):
    for _ in range(100000000):
        pass
    return i*2

print(myfunc(2))

这将打印出如下内容:

myfunc: utime 0:00:03.261688 stime 0:00:00.805324 wall 0:00:04.067109
4

如果您想要多个字段,您可能需要减去 rusage 结果的所有成员,但由于这些都是 int 或 float,所以很简单:

If you want more than a couple fields, you probably want to subtract all of the members of the rusage results, but since these are all int or float, that's easy:

rdiff = resource.struct_rusage(f1-f0 for f0, f1 in zip(r0, r))
sys.stderr.write('{}: utime {} maxrss {} nsignals {} etc.\n'.format(
    datetime.timedelta(seconds=rdiff.r_utime),
    rdiff.ru_maxrss,
    rdiff.ru_nsignals))

这篇关于如何使用资源模块来衡量一个函数的运行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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