8000 Use builtin round instead of np.round for scalars. by anntzer · Pull Request #14616 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Use builtin round instead of np.round for scalars. #14616

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
Jun 30, 2019

Conversation

anntzer
Copy link
Contributor
@anntzer anntzer commented Jun 25, 2019

Compared to np.round, builtin round is 20x faster for python floats and
2x faster for numpy floats (it's admittedly unlikely that any of the
places patched is an actual bottleneck).

On Py2 builtin round and np.round were not equivalent because builtin
round rounded halves away from zero, but now both round-to-even.

Small misc. changes; the rearrangement in backend_agg.draw_text is to
make the implementation more similar to draw_mathtext (just above).

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

Compared to np.round, builtin round is 20x faster for python floats and
2x faster for numpy floats (it's admittedly unlikely that any of the
places patched is an actual bottleneck).

On Py2 builtin round and np.round were not equivalent because builtin
round rounded halves away from zero, but now both round-to-even.

Small misc. changes; the rearrangement in backend_agg.draw_text is to
make the implementation more similar to draw_mathtext (just above).
Copy link
Member
@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

It looks like un-related name changes to an example snuck in, but that is ok with me.

@tacaswell
Copy link
Member

Are there any cases where weird scalers (from pandas or one of the unit libraries) would be correctly handled by np.round but not round? I think in those cases that should be a bug against the source of the scalar, but not 100% on that.

@anntzer
Copy link
Contributor Author
anntzer commented Jun 25, 2019

The name changes are because that example uses np.round so grepping found it... but it can't actually be changed to use builtin round there :)
Third-party objects can support builtin round by implementing __round__, so if that turns out to be a problem we can at least report it to them...

@tacaswell tacaswell added this to the v3.2.0 milestone Jun 25, 2019
@timhoffm timhoffm merged commit fdcdf4f into matplotlib:master Jun 30, 2019
@anntzer anntzer deleted the roundless branch June 30, 2019 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0