python - Performance of returning a new array and modifying a passed-in array -
[related]
in snapshot below, compare speed of
modifying existing array via piece assignment just returning new, modified arrayit seems latter faster. why should case?
edit: updated suggestions, , version uses numpy's vectorized add()
, fastest.
x + 1
create new array. y[:] = x + 1
: create new array , re-create info y
y = x + 1
: create new array , bind name y
new array. np.add(x, 1, out=y)
: don't create new array, it's fastest.
here code:
x = np.zeros(1000000) y = np.zeros_like(x) %timeit x + 1 %timeit y[:] = x + 1 %timeit np.add(x, 1, out=y)
the output:
100 loops, best of 3: 4.2 ms per loop 100 loops, best of 3: 6.83 ms per loop 100 loops, best of 3: 2.5 ms per loop
python python-2.7 numpy
No comments:
Post a Comment