/ 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

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