Skip to content

Prefer concatenate to h/vstack in simple cases. #19332

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

Merged
merged 1 commit into from
Jan 23, 2021

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Jan 21, 2021

concatenate is (much) faster than hstack, while being essentially
equivalent in the 1D case; when working on 2D arrays it can replace
vstack too.

Timings:

In [1]: u = np.arange(5)

In [2]: %timeit np.concatenate([u, u])
842 ns ± 8.16 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [3]: %timeit np.hstack([u, u])
2.43 µs ± 7.43 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [4]: u = np.arange(1000)

In [5]: %timeit np.concatenate([u, u])
1.2 µs ± 2.67 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [6]: %timeit np.hstack([u, u])
2.9 µs ± 10.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [7]: u = np.arange(10).reshape((5, 2))

In [8]: %timeit np.concatenate([u, u])
934 ns ± 19.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [9]: %timeit np.vstack([u, u])
2.54 µs ± 7.55 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [10]: u = np.arange(2000).reshape((1000, 2))

In [11]: %timeit np.concatenate([u, u])
1.61 µs ± 2.64 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [12]: %timeit np.vstack([u, u])
3.37 µs ± 17.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

PR Summary

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

right = Path(verts[(0, 5, 4, 3), :])
top = Path(np.concatenate([[(-x, 0)], verts[[1, 0, 5]], [(x, 0)]]))
bottom = Path(np.concatenate([[(-x, 0)], verts[2:5], [(x, 0)]]))
left = Path(verts[[0, 1, 2, 3]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
left = Path(verts[[0, 1, 2, 3]])
left = Path(verts[:4])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes to all

[-x, -y], [x, y])))
right = Path(np.vstack(([x, y], verts[(5, 4, 3), :], [-x, -y])))
top = Path(verts[[1, 0, 5, 4, 1]])
bottom = Path(verts[[1, 2, 3, 4]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bottom = Path(verts[[1, 2, 3, 4]])
bottom = Path(verts[1:5])

top = Path(verts[[1, 0, 5, 4, 1]])
bottom = Path(verts[[1, 2, 3, 4]])
left = Path(np.concatenate([
[(x, y)], verts[[0, 1, 2]], [(-x, -y), (x, y)]]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[(x, y)], verts[[0, 1, 2]], [(-x, -y), (x, y)]]))
[(x, y)], verts[:3], [(-x, -y), (x, y)]]))

left = Path(np.concatenate([
[(x, y)], verts[[0, 1, 2]], [(-x, -y), (x, y)]]))
right = Path(np.concatenate([
[(x, y)], verts[[5, 4, 3]], [(-x, -y)]]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[(x, y)], verts[[5, 4, 3]], [(-x, -y)]]))
[(x, y)], verts[5:2:-1], [(-x, -y)]]))

concatenate is (much) faster than hstack, while being essentially
equivalent in the 1D case; when working on 2D arrays it can replace
vstack too.
@timhoffm timhoffm added this to the v3.4.0 milestone Jan 23, 2021
@timhoffm timhoffm merged commit 1eef019 into matplotlib:master Jan 23, 2021
@anntzer anntzer deleted the concatenate branch January 23, 2021 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants