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

Comparison logic vs between? vs include? #225

Open
ydakuka opened this issue Nov 5, 2023 · 0 comments
Open

Comparison logic vs between? vs include? #225

ydakuka opened this issue Nov 5, 2023 · 0 comments

Comments

@ydakuka
Copy link
Contributor

ydakuka commented Nov 5, 2023

#!/usr/local/bin/ruby

require 'benchmark/ips'

Benchmark.ips do |x|
  value = 42

  x.report('>= && <=') { value >= 1000 && value <= 2000 }
  x.report('include?') { (1000..2000).include?(value) }
  x.report('between?') { value.between?(1000, 2000) }

  x.compare!
end
$ ruby -v
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-darwin19]

$ ruby bench.rb
Warming up --------------------------------------
            >= && <=     2.351M i/100ms
            include?     1.139M i/100ms
            between?     1.244M i/100ms
Calculating -------------------------------------
            >= && <=     23.476M (± 1.3%) i/s -    117.558M in   5.008408s
            include?     11.117M (± 8.2%) i/s -     55.807M in   5.057945s
            between?     11.021M (± 6.8%) i/s -     55.959M in   5.102033s

Comparison:
            >= && <=: 23475928.0 i/s
            include?: 11117073.1 i/s - 2.11x  slower
            between?: 11020978.6 i/s - 2.13x  slower
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

No branches or pull requests

1 participant