Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6910 > unrolled thread

Re: Why is this so much faster?

Started byKeir Rice <keirrice@gmail.com>
First post2011-06-02 21:05 -0700
Last post2011-06-03 10:03 +0200
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Re: Why is this so much faster? Keir Rice <keirrice@gmail.com> - 2011-06-02 21:05 -0700
    Re: Why is this so much faster? Peter Otten <__peter__@web.de> - 2011-06-03 10:03 +0200

#6910 — Re: Why is this so much faster?

FromKeir Rice <keirrice@gmail.com>
Date2011-06-02 21:05 -0700
SubjectRe: Why is this so much faster?
Message-ID<ef9db7d0-f479-4293-b67f-6e7f10f5721c@glegroupsg2000goo.googlegroups.com>
Terry,

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

Ran at the same speed as my 'fast' version and without the square brackets was slower.

Thanks,

Keir

[toc] | [next] | [standalone]


#6921

FromPeter Otten <__peter__@web.de>
Date2011-06-03 10:03 +0200
Message-ID<mailman.2420.1307088225.9059.python-list@python.org>
In reply to#6910
Keir Rice wrote:

> Ian, I was basing my code off Fredrik Lundh post on comparing images.
> http://effbot.org/zone/pil-comparing-images.htm

> return math.sqrt(sum([h*i*i for i,h in enumerate(histogram)]) /
> self.Area())
> 
> Ran at the same speed as my 'fast' version and without the square brackets
> was slower.

It looks like len(histogram) will always be 256. If so, you can factor out 
[i*i for i in range(256)]. For number-crunching tasks considering numpy 
might also be a good idea:

# untested
import numpy

deltas = numpy.arange(256, dtype=float)
squares = deltas * deltas

class Whatever:
    def RMSBand(self, histogram):
        return math.sqrt((squares*histogram).sum()/self.Area())

If it works this should be quite fast.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web