【发布时间】:2010-12-30 07:40:33
【问题描述】:
我正在尝试测量raw_queries(...) 的时间,到目前为止没有成功。我发现我应该使用 timeit 模块。问题是我不能(= 我不知道如何)将参数从环境传递给函数。
重要提示:在调用raw_queries之前,我们必须执行phase2()(环境初始化)。
旁注:代码在 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)。
更新:迄今为止最好的解决方案,在 unutbu 的帮助下(仅更改了什么):
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()
【问题讨论】:
标签: python performance time arguments timeit