Which is faster?

cnb circularfunc at yahoo.se
Sat Aug 30 00:26:35 EDT 2008


For a big nbr of it might matter?
Is av_grade O(n*2) and the first O(n)  when it comes to adding or is
"sum x for x in y" just traversing the list ones, accumulating the
values, it doesnt first build the list and then travese it for sum?

def averageGrade(self):
        tot = 0
        for review in self.reviews:
            tot += review.grade
        return tot / len(self.reviews)

def av_grade(self):
     return sum(review.grade for review in self.reviews) / \
              len(self.reviews)



More information about the Python-list mailing list