Skip to content

Instantly share code, notes, and snippets.

@gonz
Created March 9, 2012 18:09
Show Gist options
  • Save gonz/2007820 to your computer and use it in GitHub Desktop.
Save gonz/2007820 to your computer and use it in GitHub Desktop.
Set/Array union/intersection benchmark
require 'benchmark'
require 'set'
n = 50000
m = 5000
t = 100
r1 = (1..n).step(3)
r2 = (1..m).step(2)
s1 = Set.new(r1)
s2 = Set.new(r2)
a1 = r1.to_a
a2 = r2.to_a
Benchmark.bm(10) do |x|
GC.start
x.report('set union') { t.times{s1.union(s2)} }
GC.start
x.report('array union') { t.times{a1 | a2} }
GC.start
x.report('set int') { t.times{s1.intersection(s2)} }
GC.start
x.report('array int') { t.times{a1 & a2} }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment