# L-37 MCS 275 Wed 16 Apr 2008 : time_listcomp.py

# With profile we study the efficiency of list
# comprehensions to sum a list of random numbers.

def sumlist1(n):
   """
   Generates n random numbers uniformly
   distributed in [0,1] and prints the sum.
   """
   from random import uniform, seed
   seed(2)
   L = []
   for i in range(0,n):
      L.append(uniform(0,1))
   print sum(L)

def sumlist2(n):
   """
   Uses list comprehensions to generate n
   random numbers in [0,1] and prints the sum.
   """
   from random import uniform, seed
   seed(2)
   L = [ uniform(0,1) for x in range(0,n) ]
   print sum(L)

def main():
   """
   Profiles the two summations.
   """
   import profile
   profile.run('sumlist1(10000)')
   profile.run('sumlist2(10000)')

if __name__=="__main__": main()
