[Tutor] compile time calculator
Evert Rol
evert.rol at gmail.com
Sat Jan 28 22:17:02 CET 2012
> Well, using python documentation, I did this..
>
> (calculating execution time).
>
> def main():
> ## This is my whole puzzle code...
>
>
> fobj_ip = open('D:/code/py/input.txt', 'r')
> fobj_op = open('D:/code/py/output.txt','w')
>
> line=1
> for eachLine in fobj_ip:
> if line>1:
> fobj_op.write ("Case #%d: %d\n" %(line-1, the_count(eachLine) ) )
> line+=1
>
> pass
>
> if __name__ == '__main__':
> from timeit import Timer
> main()
> t = Timer("main()")
This, in my experience, causes problems: timeit doesn't run your main(), it runs its own timeit.main() function.
Try renaming main() to test(), and see if things still work.
> print t.timeit()
>
>
> This is showing the following output
> 10000000 loops, best of 3: 0.0612 usec per loop
>
> (This is repeating...)
>
> So, what exactly is 0.0612 usec per loop, is that execution time of my whole program??
> If, how do I calculate it in seconds.
As far as I'm aware, timeit runs the function in your Timer class (the 'main' function) 10000000 times. So it doesn't run the complete program, but assuming the main function is the entry point of the program, that's very near.
The 10000000 times is the loop; timeit will measure the total time spent looping 10000000 times through your main function, then average that.
It does this procedure 3 times, then takes the best value of three (the values could be very different, if your computer is otherwise occupied that slows down the execution of your program; hence best of three).
The 10000000 and 3 are the default values, by the way. See the timeit.Timer.repeat documentation.
So in the end, you have the average duration of a single execution of the main function, assuming the best circumstances ('best out of 3').
The usec stands for microseconds; it should actually be µs. One microsecond is 1 millionth of a second, so 0.0612 usec would be 0.0000000612 seconds for one executiong of main().
Hope that helps,
Evert
More information about the Tutor
mailing list