Skip to content

Instantly share code, notes, and snippets.

@ccocchi
Created August 17, 2017 14:37
Show Gist options
  • Save ccocchi/933cdfc6e4912307bd6789e7f4a98e13 to your computer and use it in GitHub Desktop.
Save ccocchi/933cdfc6e4912307bd6789e7f4a98e13 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
array = %w(abc def ghi jkl)
ok = %w(abc jkl)
bad = %w(abc yio)
Benchmark.ips do |x|
x.report('& ok') { (array & ok).size == ok.size }
x.report('all ok') { ok.all? { |e| array.include?(e) } }
x.report('& bad') { (array & bad).size == bad.size }
x.report('all bad') { bad.all? { |e| array.include?(e) } }
x.compare!
end
@ccocchi
Copy link
Author

ccocchi commented Aug 17, 2017

Comparison:
              all ok:  1475953.4 i/s
             all bad:  1360481.8 i/s - same-ish: difference falls within error
               & bad:  1033699.0 i/s - 1.43x  slower
                & ok:   972944.0 i/s - 1.52x  slower

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