Skip to content
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

Simplify rotate count calculation #5035

Merged
merged 1 commit into from Sep 25, 2017
Merged

Conversation

oprypin
Copy link
Member

@oprypin oprypin commented Sep 25, 2017

Behaves the same:

(1..15).each do |size|
  (-16..16).each do |n|
    m = n
    
    n %= size if n.abs >= size
    n += size if n < 0
    
    m %= size
    
    raise ":(" if n != m
  end
end

Performs the same:

require "benchmark"
Benchmark.ips do |x|
  x.report("old") do
    (1..15).each do |size|
      (-15..15).each do |n|
        n %= size if n.abs >= size
        n += size if n < 0
      end
    end
  end
  x.report("new") do
    (1..15).each do |size|
      (-15..15).each do |n|
        n %= size
      end
    end
  end
end
old 469.55M (  2.13ns) (± 3.77%)  1.00× slower
new 470.83M (  2.12ns) (± 3.44%)       fastest

@mverzilli mverzilli merged commit 13db75b into crystal-lang:master Sep 25, 2017
@mverzilli
Copy link

Thanks!

@asterite asterite added this to the Next milestone Sep 25, 2017
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.

None yet

3 participants