# L-29 MCS 260 Fri 18 Mar 2016 : time_listcomp.py

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

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

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

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

if __name__ == "__main__":
    main()
