[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

====================
#SpinSpeeds.py

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

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

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 
countUsingCounter")
     counterTime = t.timeit(repetitions)

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

========================================== 



More information about the Tutor mailing list