如何使用 Python timeit 模块捕获返回值? [英] How can I capture return value with Python timeit module?

查看:27
本文介绍了如何使用 Python timeit 模块捕获返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 for 循环中使用 sklearn 运行多种机器学习算法,想看看每个算法需要多长时间.问题是我还需要返回一个值,并且不想多次运行它,因为每个算法都需要很长时间.有没有办法使用 python 的 timeit 模块或类似的具有这样功能的模块来捕获返回值clf"...

Im running several machine learning algorithms with sklearn in a for loop and want to see how long each of them takes. The problem is I also need to return a value and DONT want to have to run it more than once because each algorithm takes so long. Is there a way to capture the return value 'clf' using python's timeit module or a similar one with a function like this...

def RandomForest(train_input, train_output):
    clf = ensemble.RandomForestClassifier(n_estimators=10)
    clf.fit(train_input, train_output)
    return clf

当我像这样调用函数时

t = Timer(lambda : RandomForest(trainX,trainy))
print t.timeit(number=1)

附言我也不想设置全局clf",因为我以后可能想进行多线程或多处理.

P.S. I also dont want to set a global 'clf' because I might want to do multithreading or multiprocessing later.

推荐答案

问题归结为timeit._template_func 不返回函数的返回值:

The problem boils down to timeit._template_func not returning the function's return value:

def _template_func(setup, func):
    """Create a timer function. Used if the "statement" is a callable."""
    def inner(_it, _timer, _func=func):
        setup()
        _t0 = _timer()
        for _i in _it:
            _func()
        _t1 = _timer()
        return _t1 - _t0
    return inner

我们可以通过一些猴子补丁来使 timeit 符合我们的意愿:

We can bend timeit to our will with a bit of monkey-patching:

import timeit
import time

def _template_func(setup, func):
    """Create a timer function. Used if the "statement" is a callable."""
    def inner(_it, _timer, _func=func):
        setup()
        _t0 = _timer()
        for _i in _it:
            retval = _func()
        _t1 = _timer()
        return _t1 - _t0, retval
    return inner

timeit._template_func = _template_func

def foo():
    time.sleep(1)
    return 42

t = timeit.Timer(foo)
print(t.timeit(number=1))

返回

(1.0010340213775635, 42)

第一个值是 timeit 结果(以秒为单位),第二个值是函数的返回值.

The first value is the timeit result (in seconds), the second value is the function's return value.

请注意,当 timeit.Timer 传递了 callable 时,上面的猴子补丁只会影响 timeit 的行为.如果您传递一个字符串语句,那么您必须(类似地)对 timeit.template 字符串进行猴子补丁.

Note that the monkey-patch above only affects the behavior of timeit when a callable is passed timeit.Timer. If you pass a string statement, then you'd have to (similarly) monkey-patch the timeit.template string.

这篇关于如何使用 Python timeit 模块捕获返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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