Use numpy.ndarray.sum() vs. __builtin__.sum() to compute hist bin counts.
authorW. Trevor King <wking@drexel.edu>
Fri, 29 Oct 2010 19:19:31 +0000 (15:19 -0400)
committerW. Trevor King <wking@drexel.edu>
Fri, 29 Oct 2010 19:19:31 +0000 (15:19 -0400)
It's much faster:
  $ time python -c 'import numpy; sum(numpy.arange(int(1e6)))'
  real    0m3.312s
  user    0m3.188s
  sys     0m0.118s
  $ time python -c 'import numpy; numpy.arange(int(1e6)).sum()'
  real    0m0.878s
  user    0m0.753s
  sys     0m0.122s

pysawsim/histogram.py

index 5b4480b856fa1abd17623e022c08ef8e8c8a49ae..bb66c05f2bb0c3eebd74cb40b67ae1bafa21ebfb 100644 (file)
@@ -69,7 +69,7 @@ class Histogram (object):
         bin_is = numpy.floor((data - self.bin_edges[0])/bin_width)
         self.counts = []
         for i in range(len(self.bin_edges)-1):
-            self.counts.append(sum(bin_is == i))
+            self.counts.append(sum(bin_is == i).sum())
         self.total = float(len(data)) # some data might be outside the bins
         self.mean = data.mean()
         self.std_dev = data.std()