8000 ENH: perform inplace operations if one operand is a temporary by juliantaylor · Pull Request #4322 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: perform inplace operations if one operand is a temporary #4322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
wip, check last instruction
benchmark:

import numpy as np
n = 10000
d = np.ones(n)
e = np.ones(n)
f = np.ones(n)

def fun(d, e, f):
    t = d + e
    t += f

%timeit -r 20 d+e+f
%timeit -r 20 fun(d, e, f)

n = 100000
d = np.ones(n)
e = np.ones(n)
f = np.ones(n)
%timeit -r 20 d+e+f
%timeit -r 20 fun(d, e, f)

n = 1000000
d = np.ones(n)
e = np.ones(n)
f = np.ones(n)
%timeit -r 20 d+e+f
%timeit -r 20 fun(d, e, f)
  • Loading branch information
juliantaylor committed Aug 6, 2014
commit 18b9ed541925161b80d9c0491ad14dde5b5cb51c
10 changes: 9 additions & 1 deletion numpy/core/src/multiarray/number.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,19 +354,27 @@ array_inplace_bitwise_or(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_bitwise_xor(PyArrayObject *m1, PyObject *m2);

PyFrameObject * last_frame = NULL;
int lasti = -1;
PyArrayObject * last_array = NULL;
/*
* check if m1 is a temporary (refcnt == 1) so we can do inplace operations
* instead of creating a new temporary
*/
static int can_elide_temp(PyArrayObject * m1, PyObject * m2)
{
PyFrameObject * f = PyEval_GetFrame();
int can = lasti != f->f_lasti && last_frame == f;
lasti = f->f_lasti;
last_frame = f;
if (!can)
return 0;
if (Py_REFCNT(m1) == 1 && /* TODO: use m2 == 1 too */
PyArray_CheckExact(m1) && PyArray_CheckExact(m2) &&
PyArray_DESCR(m1)->type_num != NPY_VOID &&
PyArray_DESCR(m1)->type_num == PyArray_DESCR(m2)->type_num &&
PyArray_NDIM(m1) == PyArray_NDIM(m2) &&
(PyArray_FLAGS(m1) & NPY_ARRAY_OWNDATA)) {
PyFrameObject * f = PyEval_GetFrame();
PyObject ** stack = f->f_valuestack;
npy_intp nstack = f->f_code->co_stacksize;
npy_intp i;
Expand Down
0