diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2013-10-19 12:43:03 +0200 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2013-10-19 12:43:03 +0200 |
commit | 81a0fe984fb5e4546a318260f8c9955440f6abf2 (patch) | |
tree | dd5e41b4b204076592495a4635b151aa288816e9 /numpy | |
parent | 18acfa462a63bcdaf86360f0c94bc9347ecafad5 (diff) | |
download | numpy-81a0fe984fb5e4546a318260f8c9955440f6abf2.tar.gz |
ENH: Inline check_and_adjust_index
The function takes a considerable amount of time of np.take and
to also fancy indexing. Simple np.takes can speed up by more then
40%, fancy indexes around 10%.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/common.c | 25 | ||||
-rw-r--r-- | numpy/core/src/multiarray/common.h | 45 |
2 files changed, 35 insertions, 35 deletions
diff --git a/numpy/core/src/multiarray/common.c b/numpy/core/src/multiarray/common.c index 9a05133a1..d67332118 100644 --- a/numpy/core/src/multiarray/common.c +++ b/numpy/core/src/multiarray/common.c @@ -609,31 +609,6 @@ _array_typedescr_fromstr(char *c_str) return descr; } -NPY_NO_EXPORT int -check_and_adjust_index(npy_intp *index, npy_intp max_item, int axis) -{ - /* Check that index is valid, taking into account negative indices */ - if ((*index < -max_item) || (*index >= max_item)) { - /* Try to be as clear as possible about what went wrong. */ - if (axis >= 0) { - PyErr_Format(PyExc_IndexError, - "index %"NPY_INTP_FMT" is out of bounds " - "for axis %d with size %"NPY_INTP_FMT, - *index, axis, max_item); - } else { - PyErr_Format(PyExc_IndexError, - "index %"NPY_INTP_FMT" is out of bounds " - "for size %"NPY_INTP_FMT, - *index, max_item); - } - return -1; - } - /* adjust negative indices */ - if (*index < 0) { - *index += max_item; - } - return 0; -} NPY_NO_EXPORT char * index2ptr(PyArrayObject *mp, npy_intp i) diff --git a/numpy/core/src/multiarray/common.h b/numpy/core/src/multiarray/common.h index 3e060de3d..44b8308f8 100644 --- a/numpy/core/src/multiarray/common.h +++ b/numpy/core/src/multiarray/common.h @@ -40,16 +40,6 @@ _array_find_python_scalar_type(PyObject *op); NPY_NO_EXPORT PyArray_Descr * _array_typedescr_fromstr(char *str); -/* - * Returns -1 and sets an exception if *index is an invalid index for - * an array of size max_item, otherwise adjusts it in place to be - * 0 <= *index < max_item, and returns 0. - * 'axis' should be the array axis that is being indexed over, if known. If - * unknown, use -1. - */ -NPY_NO_EXPORT int -check_and_adjust_index(npy_intp *index, npy_intp max_item, int axis); - NPY_NO_EXPORT char * index2ptr(PyArrayObject *mp, npy_intp i); @@ -67,6 +57,41 @@ offset_bounds_from_strides(const int itemsize, const int nd, const npy_intp *dims, const npy_intp *strides, npy_intp *lower_offset, npy_intp *upper_offset); + +/* + * Returns -1 and sets an exception if *index is an invalid index for + * an array of size max_item, otherwise adjusts it in place to be + * 0 <= *index < max_item, and returns 0. + * 'axis' should be the array axis that is being indexed over, if known. If + * unknown, use -1. + */ +static NPY_INLINE int +check_and_adjust_index(npy_intp *index, npy_intp max_item, int axis) +{ + /* Check that index is valid, taking into account negative indices */ + if ((*index < -max_item) || (*index >= max_item)) { + /* Try to be as clear as possible about what went wrong. */ + if (axis >= 0) { + PyErr_Format(PyExc_IndexError, + "index %"NPY_INTP_FMT" is out of bounds " + "for axis %d with size %"NPY_INTP_FMT, + *index, axis, max_item); + } else { + PyErr_Format(PyExc_IndexError, + "index %"NPY_INTP_FMT" is out of bounds " + "for size %"NPY_INTP_FMT, + *index, max_item); + } + return -1; + } + /* adjust negative indices */ + if (*index < 0) { + *index += max_item; + } + return 0; +} + + /* * return true if pointer is aligned to 'alignment' */ |