在 Python 中测量带参数的函数的时间函数、测量、参数、时间

2023-09-06 18:28:32 作者:o.餹菓箼℡

我正在尝试测量 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()

感谢您的帮助.

更新:我们可以使用 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()

推荐答案

首先,永远不要使用time模块对函数进行计时.很容易得出错误的结论.有关示例,请参阅 timeit 与计时装饰器.

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 变量.在您发布的代码中,queries 是在 phase2() 中定义的,但仅限于本地.因此,要将 queries 设置为全局变量,您需要执行一些操作,例如让 phase2 return 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