/ RUBY

Array comparison in ruby

I was working on a problem to check on a large array of integers to check whether it is a part of another array or not. Both the arrays have unique elements, but not in sorted order. So there are multiple ways possible to this, but I was not sure that which approach is most efficient. I searched for this but couldn’t come across anything valuable. Then I decided to benchmark whole approach by my self.

Code

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
view raw benchmark.rb hosted with ❤ by GitHub
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
view raw console.log hosted with ❤ by GitHub
arr1.to_set.subset? arr2.to_set

If we have array which is already converted in Set then susbet comparison is fastet(1.7 times) than second rank method. But if we consider conversion of Array into sets then it is the slowest method.

In above code snippet we have compared four ways to compare arrays with each other.

Buy me a coffee