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()
谢谢你的帮助。
更新:我们可以phase2()
使用Timer
. 问题是我们需要(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()