Skip to content

Instantly share code, notes, and snippets.

@timuruski
Created July 14, 2011 21:24
Show Gist options
  • Save timuruski/1083496 to your computer and use it in GitHub Desktop.
Save timuruski/1083496 to your computer and use it in GitHub Desktop.
require 'benchmark'
Test = Struct.new(:value)
def contains(a, b)
a.any? do |n|
b.include?(n)
end
end
Benchmark.bm do |x|
a = (0..10000).map { |i| Test.new(i) }
b = [Test.new(1000), Test.new(99999)]
x.report("early match, intersect") { (a & b).any? }
x.report("early match, loop") { contains(a, b) }
end
Benchmark.bm do |x|
a = (0..10000).map { |i| Test.new(i) }
b = [Test.new(99999)]
x.report("no match, intersect") { (a & b).any? }
x.report("no match, loop") { contains(a, b) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment