Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'skipping': 0.07; 'terry': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'pm,': 0.10; 'def': 0.12; 'wrote:': 0.14; 'bit.': 0.16; 'expression,': 0.16; 'reedy': 0.16; "tim's": 0.16; 'wrapping': 0.16; 'list?': 0.16; 'jan': 0.20; 'header:In-Reply-To:1': 0.21; 'repeatedly': 0.22; 'code.': 0.22; 'function': 0.25; 'tried': 0.27; 'subject:?': 0.29; 'version': 0.29; 'bit': 0.30; 'all,': 0.30; 'second': 0.30; 'yes.': 0.30; 'header:X-Complaints-To:1': 0.32; 'to:addr:python- list': 0.33; 'error': 0.33; 'question': 0.34; 'regardless': 0.34; 'showing': 0.34; 'header:User-Agent:1': 0.35; 'skip:" 10': 0.35; 'version.': 0.35; 'skip:e 20': 0.37; 'received:org': 0.38; 'subject:: ': 0.38; 'skip:s 20': 0.39; 'should': 0.39; 'map': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'skip:z 10': 0.40; 'removal': 0.65; 'response.': 0.68; 'subject:this': 0.76; 'trial': 0.76; 'stream': 0.77; 'bottle': 0.84; 'colour': 0.84; 'profiles': 0.84; 'subject:Why': 0.84; 'subject:much': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Why is this so much faster? Date: Thu, 02 Jun 2011 19:04:57 -0400 References: <8bccb0f0-4c1a-4ec8-91b4-4f420d0d8eb9@glegroupsg2000goo.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: rain.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: <8bccb0f0-4c1a-4ec8-91b4-4f420d0d8eb9@glegroupsg2000goo.googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307055911 news.xs4all.nl 49048 [::ffff:82.94.164.166]:36054 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6893 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