diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-04-19 02:26:34 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-04-19 02:26:34 +0000 |
commit | 959e147a8a41c37bc3b9d1f11d89449cb4cc9882 (patch) | |
tree | e55e7929e6a624d64944e13b1672d3ba3655ae30 /numpy/core/src/arrayobject.c | |
parent | 787e44fdc052f858679175850515259433e48ed5 (diff) | |
download | numpy-959e147a8a41c37bc3b9d1f11d89449cb4cc9882.tar.gz |
Speed up copying code a bit for well-behaved cases.
Diffstat (limited to 'numpy/core/src/arrayobject.c')
-rw-r--r-- | numpy/core/src/arrayobject.c | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index 4d0c12f18..8a8c55837 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -621,6 +621,23 @@ PyArray_Size(PyObject *op) } } + +static void +_strided_byte_copy(char *dst, intp outstrides, char *src, intp instrides, + intp N, int elsize) +{ + intp i, j; + char *tout = dst; + char *tin = src; + for (i=0; i<N; i++) { + for (j=0; j<elsize; j++) { + *tout++ = *tin++; + } + tin = tin + instrides - elsize; + tout = tout + outstrides - elsize; + } +} + /* If destination is not the right type, then src will be cast to destination. */ @@ -642,7 +659,7 @@ PyArray_CopyInto(PyArrayObject *dest, PyArrayObject *src) PyArrayIterObject *dit=NULL; PyArrayIterObject *sit=NULL; char *dptr; - int swap; + int swap, nd; PyArray_CopySwapFunc *copyswap; PyArray_CopySwapNFunc *copyswapn; @@ -689,6 +706,45 @@ PyArray_CopyInto(PyArrayObject *dest, PyArrayObject *src) PyArray_INCREF(dest); return 0; } + + /* See if we can iterate over the largest dimension */ + if (!swap && (nd = dest->nd) == src->nd && (nd > 0) && + PyArray_CompareLists(dest->dimensions, src->dimensions, nd)) { + int maxaxis=0, maxdim=dest->dimensions[0]; + int i; + for (i=1; i<nd; i++) { + if (dest->dimensions[i] > maxdim) { + maxaxis = i; + maxdim = dest->dimensions[i]; + } + } + + dit = (PyArrayIterObject *) \ + PyArray_IterAllButAxis((PyObject *)dest, maxaxis); + sit = (PyArrayIterObject *) \ + PyArray_IterAllButAxis((PyObject *)src, maxaxis); + + if ((dit == NULL) || (sit == NULL)) { + Py_XDECREF(dit); + Py_XDECREF(sit); + return -1; + } + + PyArray_XDECREF(dest); + index = dit->size; + while(index--) { + /* strided copy of elsize bytes */ + _strided_byte_copy(dit->dataptr, dest->strides[maxaxis], + sit->dataptr, src->strides[maxaxis], + maxdim, elsize); + PyArray_ITER_NEXT(dit); + PyArray_ITER_NEXT(sit); + } + PyArray_INCREF(dest); + Py_DECREF(dit); + Py_DECREF(sit); + return 0; + } dit = (PyArrayIterObject *)PyArray_IterNew((PyObject *)dest); sit = (PyArrayIterObject *)PyArray_IterNew((PyObject *)src); |