[Tutor] Calling a function

Kevin Reeder reederk at comcast.net
Thu Jun 9 08:45:59 CEST 2005


I'm having trouble with this code which is meant to run a time
comparison between two similar functions. The first module is
makezeros.py

def lots_of_appends():
    zeros = []
    for i in range(10000):		
	zeros.append(0)
		
def one_multiply():
    zeros = [0] * 10000


The second module is timings.py.

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()
	    apply(func)
	    stoptime = time.time()
	    elapsed = stoptime-starttime
	    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))


Here's the outcome I get:

$ python ./Python/timings.py
Traceback (most recent call last):
  File "./Python/timings.py", line 17, in ?
    do_timing(100, (lots_of_appends, one_multiply))
  File "./Python/timings.py", line 10, in do_timing
    apply(func)
TypeError: 'tuple' object is not callable


BTW, the code is taken straight out of Learning Python, but I've
been banging my head on it for awhile. Any ideas??

Kevin




More information about the Tutor mailing list