Skip to content

Instantly share code, notes, and snippets.

@styx
Created February 23, 2015 17:00
Show Gist options
  • Save styx/3d8a23250d899a53791f to your computer and use it in GitHub Desktop.
Save styx/3d8a23250d899a53791f to your computer and use it in GitHub Desktop.
#!/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