Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
require 'benchmark/ips'
require "set"
X = (0...10000).sort_by{rand}.take(5000)
Y = (0...10000).sort_by{rand}.take(5000)
SET_X = Set.new(X)
SET_Y = Set.new(Y)
def array
X & Y
end
def set1
SET_X & SET_Y
end
def set2
SET_X.intersection SET_Y
end
Benchmark.ips do |x|
x.report('Union by Array#&') { array }
x.report('Union by Set#&') { set1 }
x.report('Union by Set#intersection') { set2 }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment