Created
November 15, 2017 08:57
This file contains 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 'benchmark/ips' | |
require 'set' | |
arr1 = [1,2,3] | |
arr2 = [1,2,3,4] | |
s1 = arr1.to_set | |
s2 = arr2.to_set | |
Benchmark.ips do |benchmark| | |
benchmark.report('array subtraction') { (arr1 - arr2).empty?} | |
benchmark.report('and operator') { (arr1 & arr2).size == arr1.size } | |
benchmark.report('set operation') { s1.subset? s2 } | |
benchmark.report('set conversion') { arr1.to_set.subset? arr2.to_set } | |
benchmark.report('all function') { arr1.all? {|val| arr2.include? val } } | |
benchmark.compare! | |
end |
This file contains 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
Warming up -------------------------------------- | |
array subtraction 100.925k i/100ms | |
and operator 83.820k i/100ms | |
set operation 151.517k i/100ms | |
set conversion 16.651k i/100ms | |
all function 115.905k i/100ms | |
Calculating ------------------------------------- | |
array subtraction 1.574M (± 5.5%) i/s - 7.872M in 5.018468s | |
and operator 1.451M (± 4.4%) i/s - 7.292M in 5.035548s | |
set operation 3.185M (±10.3%) i/s - 15.758M in 5.028507s | |
set conversion 194.785k (± 8.8%) i/s - 965.758k in 5.015070s | |
all function 1.788M (± 4.2%) i/s - 8.925M in 5.001685s | |
Comparison: | |
set operation: 3184869.5 i/s | |
all function: 1787595.0 i/s - 1.78x slower | |
array subtraction: 1573641.1 i/s - 2.02x slower | |
and operator: 1451096.7 i/s - 2.19x slower | |
set conversion: 194784.8 i/s - 16.35x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment