Why is this so much faster?

Chris Kaynor ckaynor at zindagigames.com
Thu Jun 2 18:41:30 EDT 2011


I'm making the presumption that you are using Python 2.x in my notes.

On Thu, Jun 2, 2011 at 3:07 PM, Keir Rice <keirrice at gmail.com> wrote:

> Hi All,
>
> The following function was showing up in my profiles as a large bottle
> neck:
>
> # Slow version
> def RMSBand(self, histogram):
>        """Calculates the root-mean-squared value for the given colour
> stream histogram."""
>        intermediateResult = map(lambda (i, h): h*(i**2), zip(histogram,
> range(255)))
>
       totalSum = sum(intermediateResult)
>
> range(255) will create a list containing 255 elements.
The zip will also create a list containing 255 elements.
map will also create a list containing 255 elements.
The lambda is adding both function definition (which is probably very
minimal) and 255 function calls (which may be slower)
You are also iterating over the lists a total of 3 times (one for zip, one
for map, one for sum) than the once in your later version.

It may be that the following two lines are faster than your two lines, in
which case its the list creations:
import itertools
totalSum = sum(itertools.imap(lambda (h, i): h*(i**2), enumerate(histogram))

If the function call is significant, the following will be even faster:
totalSum = sum(h*(i**2) for (i, h) in enumerate(histogram))



>        # calculate rms
>        return math.sqrt(totalSum / self.Area())
>
> So after a bit of trial and error I came up with the following function
> which is a lot faster:
>
> # Fast version
> def RMSBand(self, histogram):
>        """Calculates the root-mean-squared value for the given colour
> stream histogram."""
>        totalSum = 0
>        for i, h in enumerate(histogram):
>                totalSum += h*(i**2)
>
>        # calculate rms
>        return math.sqrt(totalSum / self.Area())
>
> My question is why is the second method so much faster?
> Is it the removal of the extra function calls?
> Is it skipping the creation of a list?
>

My guess is its a combination of the removal of 255 function calls and the
generation of three 255-item lists (not just the one you are implying you
know about).


> Do the basic arithmetic operators get pushed up into C code?
>
> Any insights into the whats really happening behind the scenes would be
> appreciated.
>
> Keir
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110602/e979a3f6/attachment-0001.html>


More information about the Python-list mailing list