Forked from JuanitoFatas/union-array-ampersand-vs-set.rb
Last active
August 29, 2015 14:15
-
-
Save tosbourn/c9aacfbd335f15d74453 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
| require "set" | |
| require 'benchmark/ips' | |
| X = [5, 5, 1, 1] | |
| Y = [1, 2, 5] | |
| SET_X = Set.new(X) | |
| SET_Y = Set.new(Y) | |
| def fast | |
| X & Y | |
| end | |
| def slow | |
| SET_X & SET_Y | |
| end | |
| def slowest | |
| SET_X.intersection SET_Y | |
| end | |
| Benchmark.ips do |x| | |
| x.report('Union by Array#&') { fast } | |
| x.report('Union by Set#&') { slow } | |
| x.report('Union by Set#intersection') { slowest } | |
| x.compare! | |
| end |
tosbourn
commented
Feb 20, 2015
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment