From: W. Trevor King Date: Fri, 29 Oct 2010 19:19:31 +0000 (-0400) Subject: Use numpy.ndarray.sum() vs. __builtin__.sum() to compute hist bin counts. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=8bb96b14de3e4a8da0bbb3c378f84f601e9e87e6;p=sawsim.git Use numpy.ndarray.sum() vs. __builtin__.sum() to compute hist bin counts. 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 --- diff --git a/pysawsim/histogram.py b/pysawsim/histogram.py index 5b4480b..bb66c05 100644 --- a/pysawsim/histogram.py +++ b/pysawsim/histogram.py @@ -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()