Skip to content

Commit

Permalink
ENH: use fast sequence API for setArrayFromSequence
Browse files Browse the repository at this point in the history
reduces runtime of function by about 10%
  • Loading branch information
juliantaylor committed Mar 30, 2014
1 parent d36f822 commit afdb698
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,15 +503,18 @@ setArrayFromSequence(PyArrayObject *a, PyObject *s,
}
/* Copy element by element */
else {
PyObject * seq;
seq = PySequence_Fast(s, "Could not convert object to sequence");
if (seq == NULL) {
goto fail;
}
for (i = 0; i < slen; i++) {
PyObject *o = PySequence_GetItem(s, i);
if (o == NULL) {
goto fail;
}
PyObject * o = PySequence_Fast_GET_ITEM(seq, i);
if ((PyArray_NDIM(a) - dim) > 1) {
PyArrayObject * tmp =
(PyArrayObject *)array_item_asarray(dst, i);
if (tmp == NULL) {
Py_DECREF(seq);
goto fail;
}

Expand All @@ -522,11 +525,12 @@ setArrayFromSequence(PyArrayObject *a, PyObject *s,
char * b = (PyArray_BYTES(dst) + i * PyArray_STRIDES(dst)[0]);
res = PyArray_DESCR(dst)->f->setitem(o, b, dst);
}
Py_DECREF(o);
if (res < 0) {
Py_DECREF(seq);
goto fail;
}
}
Py_DECREF(seq);
}

Py_DECREF(s);
Expand Down

0 comments on commit afdb698

Please sign in to comment.