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

Faster multiple_of? method #33854

Merged
merged 1 commit into from Sep 12, 2018

Conversation

Projects
None yet
6 participants
@rigani
Contributor

rigani commented Sep 12, 2018

Summary

activesupport core extension Integer#multiple_of?

  • use faster == 0 instead of zero?
  • use faster == instead of !=

Benchmark

0.multiple_of?(0)

Warming up --------------------------------------
    new_multiple_of?    19.633M i/s -     19.747M times in 1.005812s (50.93ns/i)
        multiple_of?    15.569M i/s -     15.741M times in 1.011058s (64.23ns/i)
Calculating -------------------------------------
    new_multiple_of?    29.591M i/s -     58.899M times in 1.990465s (33.79ns/i)
        multiple_of?    22.291M i/s -     46.706M times in 2.095339s (44.86ns/i)

Comparison:
    new_multiple_of?:  29590706.2 i/s
        multiple_of?:  22290597.8 i/s - 1.33x  slower

4611686018427387903.multiple_of?(42)

Warming up --------------------------------------
    new_multiple_of?    13.721M i/s -     13.824M times in 1.007495s (72.88ns/i)
        multiple_of?    14.243M i/s -     14.374M times in 1.009176s (70.21ns/i)
Calculating -------------------------------------
    new_multiple_of?    20.231M i/s -     41.164M times in 2.034712s (49.43ns/i)
        multiple_of?    18.813M i/s -     42.729M times in 2.271264s (53.16ns/i)

Comparison:
    new_multiple_of?:  20230631.7 i/s
        multiple_of?:  18812736.4 i/s - 1.08x  slower
@rails-bot

This comment has been minimized.

Show comment
Hide comment
@rails-bot

rails-bot Sep 12, 2018

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

rails-bot commented Sep 12, 2018

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@rafaelfranca rafaelfranca merged commit 792fead into rails:master Sep 12, 2018

2 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@zuver

This comment has been minimized.

Show comment
Hide comment
@zuver

zuver Sep 17, 2018

Can anyone explain why simply reversing the ternary operands has an effect in this context?

zuver commented Sep 17, 2018

Can anyone explain why simply reversing the ternary operands has an effect in this context?

@jacortinas

This comment has been minimized.

Show comment
Hide comment
@jacortinas

jacortinas Sep 18, 2018

Contributor

@zuver I don't think changing the order of the operands is the change, but changing the type of equality checks being done and removing a method call.

Contributor

jacortinas commented Sep 18, 2018

@zuver I don't think changing the order of the operands is the change, but changing the type of equality checks being done and removing a method call.

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