On 6/2/2011 6:07 PM, Keir Rice 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)
        
        # 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?

Yes. Map is only 'fast' when one already has a function and is going to call it repeatedly regardless of the other code. When one has an expression, wrapping it as a function to use map is surely slower. Have you tried

  return math.sqrt(sum([h*i*i for i,h in enumerate(histogram)])
    / self.Area())

or same without [] brackets?

i*i should be faster than i**2 in any version.

Is it skipping the creation of a list?

A bit.

See Tim's response.

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to