[Tutor] What's the problem with my SpinSpeeds.py?

Dick Moores rdm at rcblue.com
Sun Aug 1 22:35:28 CEST 2004

My thanks to dragonfirebane, Alan Gauld, and Kent Johnson for their help 
and guidance.  I don't  really grasp yet what's going on, but here's what 
I have now. It incorporates all of their suggestions.   --Dick


def countUsingRange(n):
     for i in range(n):

def countUsingXrange(n):
     for i in xrange(n):

def countUsingCounter(n):
     c = 0
     while c < n:
         c += 1

def getCountAndRepetitionsFromUser():
     print "Enter count and repetitions as, e.g., 900 10000"
     n, repetitions = raw_input("Enter count and repetitions: ").split()
     return int(n), int(repetitions)

if __name__=='__main__':
     from timeit import Timer
     n, repetitions = getCountAndRepetitionsFromUser()

     t = Timer("countUsingRange(n)", "from __main__ import 
countUsingRange, n")
     rangeTime = t.timeit(repetitions)

     cmd = "countUsingXrange(%d)" % n
     t = Timer(cmd, "from __main__ import countUsingXrange")
     xrangeTime = t.timeit(repetitions)

     t = Timer("countUsingCounter(%d)" % n, "from __main__ import 
     counterTime = t.timeit(repetitions)

     print " range(%d) time = %f for %d repetitions" % (n, rangeTime, 
     print "xrange(%d) time = %f for %d repetitions" % (n, xrangeTime, 
     print "counter %d time = %f for %d repetitions" % (n, counterTime, 


More information about the Tutor mailing list