[Tutor] Why does counting to 20 million stress my computer?

Dick Moores rdm at rcblue.com
Wed Jul 21 06:51:04 CEST 2004


Alan Gauld wrote at 10:53 7/20/2004:
> >      for k in range(max):
>
>This line creates a list of max numbers. Each number takes up
>several bytes of RAM(4+?). So 20 million numbers is over
>80MB RAM being used.
>
>You probably should investigate generators for this kind of
>thing, or at least use xrange() instread of range()
>
>Alan G.

Yes, I went with xrange(). But you've got me curious. I looked up 
generators in _Learning Python_, 2nd ed. Not sure I understand them, but 
would you expect using a generator to be a faster way to count than using 
xrange()? Could you give me an example that would fit my spin3.py below?

Thank you,

Dick Moores

==========================================
#spin3.py

import time
print """
    Enter a positive integer n to count to n millions from zero.
    The counting will be done in two separate ways,
    and both will be timed.
    To quit, enter x or q at the prompt.
    """

while True:
     # for exiting via ^C or ^D
     try:
         max = raw_input("positive integer: ")
     except (TypeError, EOFError):
         print "Bye."
         break
     if len(max) == 0:
         print "Hey, don't just hit Enter, type an integer first!"
         continue
     if max in ["q", "x"]:
         print "Bye."
         break
     try:
         max = int(max) * 1000000 + 1
     except:
         print "That's not an integer!"
         continue
     if max <= 0:
         print "That's not a positive integer!"
         continue
     print "Counting.."
     tStart = time.time()
     for k in xrange(max):
         pass
     tEnd = time.time()
     print "0 to %d in %.3f seconds!" % (k, (tEnd - tStart))

     print "And now counting using a different loop.."
     c = 0
     tStart = time.time()
     while c < max -1 :
         c += 1
     tEnd = time.time()
     print "0 to %d in %.3f seconds!" % (c, (tEnd - tStart))
======================================




-------------- next part --------------
#spin3.py

import time
print """
   Enter a positive integer n to count to n millions from zero.
   The counting will be done in two separate ways,
   and both will be timed.
   To quit, enter x or q at the prompt.
   """

while True:
    # for exiting via ^C or ^D
    try:
        max = raw_input("positive integer: ")
    except (TypeError, EOFError):
        print "Bye."
        break
    if len(max) == 0:
        print "Hey, don't just hit Enter, type an integer first!"
        continue
    if max in ["q", "x"]:
        print "Bye."
        break
    try:
        max = int(max) * 1000000 + 1
    except:
        print "That's not an integer!"
        continue
    if max <= 0:
        print "That's not a positive integer!"
        continue
    print "Counting.."
    tStart = time.time()
    for k in xrange(max):
        pass
    tEnd = time.time()
    print "0 to %d in %.3f seconds!" % (k, (tEnd - tStart))

    print "And now counting using a different loop.."
    c = 0
    tStart = time.time()
    while c < max -1 :
        c += 1
    tEnd = time.time()
    print "0 to %d in %.3f seconds!" % (c, (tEnd - tStart))
        
        

    


More information about the Tutor mailing list