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

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

问题描述

我在sklearn中的for循环中运行了几种机器学习算法,并希望了解它们各自花费的时间.问题是我还需要返回一个值,并且DONT希望多次运行它,因为每种算法都花很长时间.有没有一种方法可以使用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)

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

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

推荐答案

问题归结为

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)

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

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

请注意,当传递 callable timeit.Timer时,上面的猴子补丁只会影响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天全站免登陆