[Tutor] timing functions
Andy W
toodles@yifan.net
Fri, 8 Mar 2002 19:15:24 +0800
Hi Kevin
The code breaks, because of the arguments it expects. You can do one of two
[simple] things to fix it.
The function is defined to receive the arguments (num_times, *funcs)
*funcs accepts any arguments after num_times.
So the first solution would be to change the function call:
do_timing(100, makezeros.lots_of_appends, makezeros.one_multiply)
#note that the functions aren't in a tuple anymore. *funcs is a tuple
of all the arguments after num_times
The second solution would be to change the function definition, and keep the
function call the same.
Like so:
def do_timing(num_times, funcs):
#Now the functions must be in a sequence
HTH
Andy W
> import time, makezeros
>
> def do_timing(num_times, *funcs):
> totals = {}
> for func in funcs: totals[func] = 0.0
> for x in range(num_times):
> for func in funcs:
> starttime = time.time() # record starting time
> apply(func)
> stoptime = time.time() # record ending time
> elapsed = stoptime-starttime # difference yields time
elapsed
> totals[func] = totals[func] + elapsed
> for func in funcs:
> print "Running %s %d times took %.3f seconds" % (func.__name__,
> num_times,
> totals[func])
>
> do_timing(100, (makezeros.lots_of_appends, makezeros.one_multiply))
>
> # ftimer.do_timing(100, (ftimer.lots_of_appends, ftimer.one_multiply))
>
>
> # csh> python timings.py
> # Running lots_of_appends 100 times took 7.891 seconds
> # Running one_multiply 100 times took 0.120 seconds
> #
> #
> # >>> import profile
> # >>> from timings import *
> # >>> from makezeros import *
> # >>> profile.run('do_timing(100, (lots_of_appends, one_multiply))')
> # Running lots_of_appends 100 times took 8.773 seconds
> # Running one_multiply 100 times took 0.090 seconds
> # ...
> #
>
>
>
>
> 2,000,000,000 Web Pages--you only need 1. Save time with My Lycos.
> http://my.lycos.com
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>