[Numpy-discussion] MA and iterators
Andrew McNamara
andrewm at object-craft.com.au
Mon Feb 7 00:24:28 CST 2005
I did some benchmarking today and discovered that iterating over MA
arrays is 14 times slower than iterating over a Numeric array (or a
python list). One simple solution to this is to add iterator support to
the MaskedArray class. To prove the principle, I patched MaskedArray on
the fly:
import MA
def ma__iter__(self):
"Get an MA iterator."
def scaler_iter(d, m):
for i, v in enumerate(d):
if m[i]:
v = masked
yield v
def array_iter(d, m, fill_value):
ss = d.spacesaver()
tc = d.typecode()
for i, v in enumerate(d):
yield MA.array(v, typecode=tc, copy=1, savespace=ss,
mask=m[i], fill_value=fill_value)
if self._mask is None:
return iter(self._data)
if len(self.shape) > 1:
return array_iter(self._data, self._mask)
else:
return scalar_iter(self._data, self._mask, self.fill_value())
MA.MaskedArray.__iter__ = ma__iter__
Note that this implementation requires generator support.
--
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/
More information about the Numpy-discussion
mailing list