Author: Maciej Fijalkowski <fij...@gmail.com> Branch: Changeset: r58543:e5eb07255e76 Date: 2012-10-28 19:22 +0100 http://bitbucket.org/pypy/pypy/changeset/e5eb07255e76/
Log: some speedups diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py b/pypy/module/micronumpy/arrayimpl/concrete.py --- a/pypy/module/micronumpy/arrayimpl/concrete.py +++ b/pypy/module/micronumpy/arrayimpl/concrete.py @@ -381,6 +381,8 @@ class ConcreteArray(BaseConcreteArray): def __init__(self, shape, dtype, order, strides, backstrides): make_sure_not_resized(shape) + make_sure_not_resized(strides) + make_sure_not_resized(backstrides) self.shape = shape self.size = support.product(shape) * dtype.get_size() self.storage = dtype.itemtype.malloc(self.size) @@ -389,8 +391,8 @@ self.strides = strides self.backstrides = backstrides - def create_iter(self, shape): - if shape == self.get_shape(): + def create_iter(self, shape=None): + if shape is None or shape == self.get_shape(): return ConcreteArrayIterator(self) r = calculate_broadcast_strides(self.strides, self.backstrides, self.get_shape(), shape) @@ -426,8 +428,8 @@ def fill(self, box): loop.fill(self, box.convert_to(self.dtype)) - def create_iter(self, shape): - if shape != self.get_shape(): + def create_iter(self, shape=None): + if shape is not None and shape != self.get_shape(): r = calculate_broadcast_strides(self.strides, self.backstrides, self.get_shape(), shape) return MultiDimViewIterator(self.parent, diff --git a/pypy/module/micronumpy/arrayimpl/scalar.py b/pypy/module/micronumpy/arrayimpl/scalar.py --- a/pypy/module/micronumpy/arrayimpl/scalar.py +++ b/pypy/module/micronumpy/arrayimpl/scalar.py @@ -34,7 +34,7 @@ def get_shape(self): return [] - def create_iter(self, shape): + def create_iter(self, shape=None): return ScalarIterator(self.value) def get_scalar_value(self): diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py --- a/pypy/module/micronumpy/interp_numarray.py +++ b/pypy/module/micronumpy/interp_numarray.py @@ -190,7 +190,7 @@ return space.call_function(cache.w_array_str, self) def dump_data(self): - i = self.create_iter(self.get_shape()) + i = self.create_iter() first = True dtype = self.get_dtype() s = StringBuilder() @@ -206,8 +206,6 @@ return s.build() def create_iter(self, shape=None): - if shape is None: - shape = self.get_shape() return self.implementation.create_iter(shape) def create_axis_iter(self, shape, dim): @@ -396,7 +394,7 @@ if self.get_size() > 1: raise OperationError(space.w_ValueError, space.wrap( "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()")) - iter = self.create_iter(self.get_shape()) + iter = self.create_iter() return space.wrap(space.is_true(iter.getitem())) def _binop_impl(ufunc_name): @@ -681,7 +679,7 @@ if ndmin > len(shape): shape = [1] * (ndmin - len(shape)) + shape arr = W_NDimArray.from_shape(shape, dtype, order=order) - arr_iter = arr.create_iter(arr.get_shape()) + arr_iter = arr.create_iter() for w_elem in elems_w: arr_iter.setitem(dtype.coerce(space, w_elem)) arr_iter.next() diff --git a/pypy/module/micronumpy/loop.py b/pypy/module/micronumpy/loop.py --- a/pypy/module/micronumpy/loop.py +++ b/pypy/module/micronumpy/loop.py @@ -89,7 +89,7 @@ reds = ['obj', 'obj_iter', 'cur_value']) def compute_reduce(obj, calc_dtype, func, done_func, identity): - obj_iter = obj.create_iter(obj.get_shape()) + obj_iter = obj.create_iter() if identity is None: cur_value = obj_iter.getitem().convert_to(calc_dtype) obj_iter.next() @@ -109,7 +109,7 @@ return cur_value def fill(arr, box): - arr_iter = arr.create_iter(arr.get_shape()) + arr_iter = arr.create_iter() while not arr_iter.done(): arr_iter.setitem(box) arr_iter.next() @@ -159,7 +159,7 @@ def do_axis_reduce(shape, func, arr, dtype, axis, out, identity): out_iter = out.create_axis_iter(arr.get_shape(), axis) - arr_iter = arr.create_iter(arr.get_shape()) + arr_iter = arr.create_iter() if identity is not None: identity = identity.convert_to(dtype) shapelen = len(shape) @@ -192,7 +192,7 @@ result = 0 idx = 1 dtype = arr.get_dtype() - iter = arr.create_iter(arr.get_shape()) + iter = arr.create_iter() cur_best = iter.getitem() iter.next() shapelen = len(arr.get_shape()) diff --git a/pypy/module/micronumpy/strides.py b/pypy/module/micronumpy/strides.py --- a/pypy/module/micronumpy/strides.py +++ b/pypy/module/micronumpy/strides.py @@ -15,16 +15,22 @@ jit.isconstant(len(chunks)) ) def calculate_slice_strides(shape, start, strides, backstrides, chunks): - rstrides = [] - rbackstrides = [] + size = 0 + for chunk in chunks: + if chunk.step != 0: + size += 1 + rstrides = [0] * size + rbackstrides = [0] * size rstart = start - rshape = [] + rshape = [0] * size i = -1 + j = 0 for i, chunk in enumerate_chunks(chunks): if chunk.step != 0: - rstrides.append(strides[i] * chunk.step) - rbackstrides.append(strides[i] * (chunk.lgt - 1) * chunk.step) - rshape.append(chunk.lgt) + rstrides[j] = strides[i] * chunk.step + rbackstrides[j] = strides[i] * (chunk.lgt - 1) * chunk.step + rshape[j] = chunk.lgt + j += i rstart += strides[i] * chunk.start # add a reminder s = i + 1 @@ -255,19 +261,19 @@ cur_step = steps[oldI] n_old_elems_to_use *= old_shape[oldI] assert len(new_strides) == len(new_shape) - return new_strides + return new_strides[:] def calculate_dot_strides(strides, backstrides, res_shape, skip_dims): - rstrides = [] - rbackstrides = [] - j=0 + rstrides = [0] * len(res_shape) + rbackstrides = [0] * len(res_shape) + j = 0 for i in range(len(res_shape)): if i in skip_dims: - rstrides.append(0) - rbackstrides.append(0) + rstrides[i] = 0 + rbackstrides[i] = 0 else: - rstrides.append(strides[j]) - rbackstrides.append(backstrides[j]) + rstrides[i] = strides[j] + rbackstrides[i] = backstrides[j] j += 1 return rstrides, rbackstrides _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit