衡量一个函数的时间在Python参数 [英] Measure time of a function with arguments in Python

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

问题描述

我试图来衡量 raw_queries的时间(...),失败为止。我发现,我应该使用timeit模块。问题是,我不能(=我不知道如何)传递参数,从环境的功能。

重要提示:拨打电话之前 raw_queries ,我们必须执行阶段2()(环境初始化)

边注:code是在Python 3

 高清raw_queries(查询,NLP):
    提交查询没有得到视觉反应    在查询问:
        nlp.query(q)的高清evaluate_queries(查询,NLP):
    测量,该查询需要返回结果的时候    T =定时器(raw_queries(查询,NLP),?????)
    打印(t.timeit())DEF阶段2():
    载入字典存储,并随后提交查询    #prepare语言处理器来提交查询
    all_files = get_files()
    B = LinguisticProcessor(all_files)
    b.loadDictionary()    #加载查询
    queries_file ='queries.txt
    查询= load_queries(queries_file)如果__name__ =='__main__':
    阶段2()

感谢您的帮助。

更新:使用定时的第二个参数我们可以称之为阶段2()。问题是,我们需要的参数(查询,NLP)从环境。

更新:到目前为止最好的解决方案,与unutbu的帮助下(仅发生了什么变化):

  DEF evaluate_queries():
    测量,该查询需要返回结果的时候    T =定时器(main.raw_queries(查询,NLP),进口为主; \\
        (查询,NLP)= main.phase2())    SF ='执行时间:{}毫秒
    打印(sf.format(t.timeit(数量= 1000)))
DEF阶段2():
    ...    返回的查询,B
高清的main():
    evaluate_queries()如果__name__ =='__main__':
    主要()


解决方案

首先,切勿使用时间模块时间的函数。这很容易导致错误的结论。见<一href=\"http://stackoverflow.com/questions/1622943/timeit-versus-timing-decorator\">http://stackoverflow.com/questions/1622943/timeit-versus-timing-decorator一个例子。

要时间的函数调用是使用IPython的的%timeit命令的最简单方法。
在那里,你只需启动一个交互式会话IPython中,调用阶段2(),定义查询
然后运行

 %timeit raw_queries(查询,NLP)

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

 蟒蛇-mtimeit -s进口检验;查询= test.phase2(),test.raw_queries(查询)

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

这里的成语是

 蟒蛇-mtimeit -sSETUP_COMMANDSCOMMAND_TO_BE_TIMED

要能够通过查询 raw_queries 函数调用,你必须定义查询变量。在code您发布查询定义阶段2(),但只能在本地。因此,要建立查询作为一个全局变量,你需要做的事情等已经 2阶段收益查询

  DEF阶段2():
    ...
    返回查询

如果你不想弄糟 2阶段这样,创建一个虚拟功能:

  DEF三期():
    #做的东西一样阶段2(),但返回的查询
    返回查询

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.

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

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()

Thanks for any help.

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

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 http://stackoverflow.com/questions/1622943/timeit-versus-timing-decorator for an example.

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)

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)"

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

The idiom here is

python -mtimeit -s"SETUP_COMMANDS" "COMMAND_TO_BE_TIMED"

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

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天全站免登陆