You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
column_stack([a, a]) always slower than concatenate([a[:,None], a[:,None]], axis=1)? #8082
I was interested in what's the fastest method of gluing two vectors together to form an n-times-2 array. To this end, I wrote a little performance testing script, see http://stackoverflow.com/a/39638773/353337.
Result:
Aside from the fact that there – unfortunately – doesn't seem to be a best solution for all vector sizes, I noticed that column_stack([a, a]) is always slower than concatenate([a[:,None], a[:,None]], axis=1). Perhaps the implementation of column_stack can be adapted accordingly.
All of these methods use concatenate under the hood.
The difference between column_stack and concatenate is simply Python overhead. 10us is unlikely to be a meaningful difference unless you are stacking very small vectors in your inner-most loop.
vstack is faster in your micro-benchmark for large arrays because it outputs an array with a different order (Fortran rather than C), and it's faster to copy arrays into contiguous rather than strided blocks. Depending on what your next operations are, this may or may be an overall performance win.
By default I would simply recommend numpy.stack, though note that it's performance is going be basically identical.
I will close this, don't see it going anywhere. There is hardly a poitn in optimizing miniscule python overhead difference. Of course concatenate is the fastest if all other functions end up calling concatenate....
Activity
shoyer commentedon Sep 22, 2016
All of these methods use
concatenateunder the hood.The difference between
column_stackandconcatenateis simply Python overhead. 10us is unlikely to be a meaningful difference unless you are stacking very small vectors in your inner-most loop.vstackis faster in your micro-benchmark for large arrays because it outputs an array with a different order (Fortran rather than C), and it's faster to copy arrays into contiguous rather than strided blocks. Depending on what your next operations are, this may or may be an overall performance win.By default I would simply recommend
numpy.stack, though note that it's performance is going be basically identical.seberg commentedon Sep 22, 2016
I will close this, don't see it going anywhere. There is hardly a poitn in optimizing miniscule python overhead difference. Of course concatenate is the fastest if all other functions end up calling concatenate....