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 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()
, definequeries
, and then runThe second easiest way that I know to use timeit is to call it from the command-line:
(In the command above, I assume the script is called
test.py
)The idiom here is
To be able to pass
queries
to theraw_queries
function call, you have to define thequeries
variable. In the code you postedqueries
is defined inphase2()
, but only locally. So to setupqueries
as a global variable, you need to do something like havephase2
returnqueries
:If you don't want to mess up
phase2
this way, create a dummy function:You don't say so, but are you by any chance trying to make the code go faster? If so, I suggest you not focus in on a particular routine and try to time it. Even if you get a number, it won't really tell you what to fix. If you can pause the program under the IDE several times and examine it's state, including the call stack, it will tell you what is taking the time and why. Here is a link that gives a brief explanation of how and why it works.*
*When you follow the link, you may have to go the the bottom of the previous page of answers. SO is having trouble following a link to an answer.
Custom timer function may be a solution:
Using like this:
It means that
sin
returned0.479...
and it took6.9e-6
seconds. Make sure your functions run long enough if you want to obtain reliable numbers (not like in the example above).I'm not sure about this, I've never used it, but from what I've read it should be something like this:
I took this from http://docs.python.org/library/timeit.html (if this helps).
Normally, you would use timeit.
Examples are here and here.
Also Note:
Or you can write your own custom timer using the time module.
If you go with a custom timer, remember that you should use time.clock() on windows and time.time() on other platforms. (timeit chooses internally)