Created
February 23, 2015 17:00
-
-
Save styx/3d8a23250d899a53791f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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