Skip to content

Commit 6168ded

Browse files
committed
optimized iteration speed over LArray, Group and Axis
1 parent cf3b796 commit 6168ded

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

larray/core/array.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
from larray.core.axis import Axis, AxisReference, AxisCollection, X, _make_axis
6565
from larray.util.misc import (table2str, size2str, basestring, izip, rproduct, ReprString, duplicates,
6666
float_error_handler_factory, _isnoneslice, light_product, unique_list, common_type,
67-
renamed_to, deprecate_kwarg, LHDFStore, lazy_attribute)
67+
renamed_to, deprecate_kwarg, LHDFStore, lazy_attribute, PY2)
6868
from larray.util.options import _OPTIONS, DISPLAY_MAXLINES, DISPLAY_EDGEITEMS, DISPLAY_WIDTH, DISPLAY_PRECISION
6969

7070

@@ -312,20 +312,21 @@ def concat(arrays, axis=0, dtype=None):
312312

313313
class LArrayIterator(object):
314314
def __init__(self, array):
315-
self.array = array
316-
self.index = 0
315+
data_iter = iter(array.data)
316+
self.nextfunc = data_iter.next if PY2 else data_iter.__next__
317+
self.axes = array.axes[1:]
317318

318319
def __iter__(self):
319320
return self
320321

321322
def __next__(self):
322-
array = self.array
323-
if self.index == len(self.array):
324-
raise StopIteration
325-
# result = array.i[array.axes[0].i[self.index]]
326-
result = array.i[self.index]
327-
self.index += 1
328-
return result
323+
data = self.nextfunc()
324+
axes = self.axes
325+
if len(axes):
326+
return LArray(data, axes)
327+
else:
328+
return data
329+
329330
# Python 2
330331
next = __next__
331332

@@ -2327,7 +2328,11 @@ def __str__(self):
23272328
__repr__ = __str__
23282329

23292330
def __iter__(self):
2330-
return LArrayIterator(self)
2331+
# fast path for 1D arrays where we return elements
2332+
if self.ndim <= 1:
2333+
return iter(self.data)
2334+
else:
2335+
return LArrayIterator(self)
23312336

23322337
def __contains__(self, key):
23332338
return any(key in axis for axis in self.axes)

larray/core/axis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ def __len__(self):
721721
return self._length
722722

723723
def __iter__(self):
724-
return iter([self.i[i] for i in range(self._length)])
724+
return iter([IGroup(i, None, self) for i in range(self._length)])
725725

726726
def __getitem__(self, key):
727727
"""

larray/core/group.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,8 @@ def __len__(self):
869869
def __iter__(self):
870870
# XXX: use translate/IGroup instead, so that it works even in the presence of duplicate labels
871871
# possibly, only if axis is set?
872-
return iter([LGroup(v, axis=self.axis) for v in self.eval()])
872+
axis = self.axis
873+
return iter([LGroup(v, axis=axis) for v in self.eval()])
873874

874875
def named(self, name):
875876
"""Returns group with a different name.

0 commit comments

Comments
 (0)