Which is faster?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Aug 30 09:11:33 CEST 2008


En Sat, 30 Aug 2008 03:15:30 -0300, Steven D'Aprano  
<steve at remove-this-cybersource.com.au> escribi�:

> On Fri, 29 Aug 2008 21:26:35 -0700, cnb wrote:
>
>> 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)
>
> Re-writing the functions so they can be tested alone:
>
> def averageGrade(alist):
>     tot = 0.0
>     for x in alist:
>         tot += x
>     return tot/len(alist)
>
>
> def av_grade(alist):
>     return sum(alist)/len(alist)
>
>
>>>> from timeit import Timer
>>>> # small amount of items
> ... alist = range(100)
>>>> Timer('averageGrade(alist)',
> ... 'from __main__ import alist, averageGrade').repeat(number=100000)
> [3.9559240341186523, 3.4910569190979004, 3.4856188297271729]
>>>>
>>>> Timer('av_grade(alist)',
> ... 'from __main__ import alist, av_grade').repeat(number=100000)
> [2.0255107879638672, 1.0968310832977295, 1.0733180046081543]
>
>
> The version with sum() is much faster. How about with lots of data?
>
>>>> alist = xrange(1000000)
>>>> Timer('averageGrade(alist)',
> ... 'from __main__ import alist, averageGrade').repeat(number=50)
> [17.699107885360718, 18.182793140411377, 18.651514053344727]
>>>>
>>>> Timer('av_grade(alist)',
> ... 'from __main__ import alist, av_grade').repeat(number=50)
> [17.125216007232666, 15.72636890411377, 16.309713840484619]
>
> sum() is still a little faster.

Mmm, in this last test you're measuring the long integer operations  
performance (because the sum exceeds largely what can be represented in a  
plain integer). Long integers are so slow that the difference between both  
loops becomes negligible.

I've tried again using float values:
alist = [float(x) for x in xrange(1000000)]
and got consistent results for any input size (the version using sum() is  
about twice as fast as the for loop)

-- 
Gabriel Genellina




More information about the Python-list mailing list