Skip to content

column_stack([a, a]) always slower than concatenate([a[:,None], a[:,None]], axis=1)? #8082

Closed

Description

@nschloe

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:

figure_1

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.

Activity

  1. shoyer commented on Sep 22, 2016

    @shoyer
    Member

    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.

  2. seberg commented on Sep 22, 2016

    @seberg
    Member

    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....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions