Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@anntzer
Copy link
Contributor

@anntzer anntzer commented Dec 7, 2019

On scalars (whether python or numpy scalars), builtin abs is ~10x faster
than np.abs:

In [1]: nf = np.float64(1); pf = 1.

In [2]: %timeit abs(nf)
156 ns ± 1.87 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [3]: %timeit abs(pf)
63.5 ns ± 0.659 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [4]: %timeit np.abs(nf)
1.26 µs ± 9.29 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [5]: %timeit np.abs(pf)
809 ns ± 4.34 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

so replace np.abs by abs in that case. (For arrays they have ~ the same
speed, so we could even do a global search-replace, but heh.)

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

I wouldn't bother about nanosecond improvements. They won't be noticeable. What convinces me on this PR is the increased readability of abs() and some simplifications along the way.

Please rebase, because this really should pass CI.

@tacaswell tacaswell added this to the v3.3.0 milestone Dec 7, 2019
On scalars (whether python or numpy scalars), builtin abs is ~10x faster
than np.abs:

    In [1]: nf = np.float64(1); pf = 1.

    In [2]: %timeit abs(nf)
    156 ns ± 1.87 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

    In [3]: %timeit abs(pf)
    63.5 ns ± 0.659 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

    In [4]: %timeit np.abs(nf)
    1.26 µs ± 9.29 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

    In [5]: %timeit np.abs(pf)
    809 ns ± 4.34 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

so replace np.abs by abs in that case.  (For arrays they have ~ the same
speed, so we could even do a global search-replace, but heh.)
@timhoffm timhoffm merged commit 099c168 into matplotlib:master Dec 8, 2019
@anntzer anntzer deleted the absless branch December 8, 2019 10:02
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.

4 participants