In Ruby, I am building a method which constructs and returns a (probably large) array which should contain no duplicate elements. Would I get better performance by using a set and then converting that to an array? Or would it be better to just call .uniq on the array I am using before I return it? Or what about using & to append items to the array instead of +=? And if I do use a set, would not having a <=> method on the object I am putting into the set have an effect on performance? (If you’re not sure, do you know of a way to test this?)
In Ruby, I am building a method which constructs and returns a (probably large)
Share
The real answer is: write the most readable and maintainable code, and optimize it only after you’ve shown it is a bottleneck. If you can find an algorithm in that is in linear time, you won’t have to optimize it. Here it’s easy to find…
Not quite sure which methods you are suggesting, but using my
fruitygem:Clearly, it makes no sense building intermediate arrays like in the third method. Otherwise, it’s not going to make a big difference since you will be in
O(n); that’s the main thing.BTW, both
sets,uniqandArray#|useeql?andhashon your objects, not<=>. These need to be defined in a sane manner, because the default is that objects are nevereql?unless they have the sameobject_id(see this question)