在 Python 中使用参数测量函数的时间 [英] Measure time of a function with arguments in Python

查看:31
本文介绍了在 Python 中使用参数测量函数的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测量 raw_queries(...) 的时间,但到目前为止未成功.我发现我应该使用 timeit 模块.问题是我不能(=我不知道如何)将参数从环境传递给函数.

I am trying to measure the time of raw_queries(...), unsuccessfully so far. I found that I should use the timeit module. The problem is that I can't (= I don't know how) pass the arguments to the function from the environment.

重要提示:在调用raw_queries之前,我们必须执行phase2()(环​​境初始化).

Important note: Before calling raw_queries, we have to execute phase2() (environment initialization).

旁注:代码在 Python 3 中.

Side note: The code is in Python 3.

def raw_queries(queries, nlp):
    """ Submit queries without getting visual response """

    for q in queries:
        nlp.query(q)

def evaluate_queries(queries, nlp):
    """ Measure the time that the queries need to return their results """

    t = Timer("raw_queries(queries, nlp)", "?????")
    print(t.timeit())

def phase2():
    """ Load dictionary to memory and subsequently submit queries """

    # prepare Linguistic Processor to submit it the queries
    all_files = get_files()
    b = LinguisticProcessor(all_files)
    b.loadDictionary()

    # load the queries
    queries_file = 'queries.txt'
    queries = load_queries(queries_file)

if __name__ == '__main__':
    phase2()

感谢您的帮助.

UPDATE:我们可以使用Timer 的第二个参数调用phase2().问题是我们需要来自环境的参数 (queries, nlp).

UPDATE: We can call phase2() using the second argument of Timer. The problem is that we need the arguments (queries, nlp) from the environment.

更新:迄今为止最好的解决方案,在 unutbu 的帮助下(仅更改了内容):

UPDATE: The best solution so far, with unutbu's help (only what has changed):

def evaluate_queries():
    """ Measure the time that the queries need to return their results """

    t = Timer("main.raw_queries(queries, nlp)", "import main;\
        (queries,nlp)=main.phase2()")

    sf = 'Execution time: {} ms'
    print(sf.format(t.timeit(number=1000)))


def phase2():
    ...

    return queries, b


def main():
    evaluate_queries()

if __name__ == '__main__':
    main()

推荐答案

首先,永远不要使用时间模块来计时功能.很容易得出错误的结论.请参阅时间与时间装饰器的示例.

First, never use the time module to time functions. It can easily lead to wrong conclusions. See timeit versus timing decorator for an example.

为函数调用计时的最简单方法是使用 IPython 的 %timeit 命令.在那里,您只需启动一个交互式 IPython 会话,调用 phase2(),定义 queries,然后运行

The easiest way to time a function call is to use IPython's %timeit command. There, you simply start an interactive IPython session, call phase2(), define queries, and then run

%timeit raw_queries(queries,nlp)

我知道使用 timeit 的第二种最简单的方法是从命令行调用它:

The second easiest way that I know to use timeit is to call it from the command-line:

python -mtimeit -s"import test; queries=test.phase2()" "test.raw_queries(queries)"

(在上面的命令中,我假设脚本名为 test.py)

(In the command above, I assume the script is called test.py)

这里的成语是

python -mtimeit -s"SETUP_COMMANDS" "COMMAND_TO_BE_TIMED"

为了能够将queries 传递给raw_queries 函数调用,您必须定义queries 变量.在您发布的代码中,queriesphase2() 中定义,但仅在本地定义.因此,要将 queries 设置为全局变量,您需要执行诸如让 phase2 返回 queries 之类的操作:

To be able to pass queries to the raw_queries function call, you have to define the queries variable. In the code you posted queries is defined in phase2(), but only locally. So to setup queries as a global variable, you need to do something like have phase2 return queries:

def phase2():
    ...
    return queries

如果你不想这样弄乱phase2,创建一个虚拟函数:

If you don't want to mess up phase2 this way, create a dummy function:

def phase3():
    # Do stuff like phase2() but return queries
    return queries

这篇关于在 Python 中使用参数测量函数的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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