Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiencies of set operations vs array operations in ruby

What are the differences in efficiencies between set and array for operations?

Examples:

  • lookups
  • iterations
  • includes?
like image 336
meow Avatar asked Feb 24 '11 05:02

meow


1 Answers

In Ruby, Set is written using an underlying Hash for its storage, and it should generally perform equivalent to a Hash. Thus:

  • include?: O(1) for Set, O(n) for Array
  • enumerations: O(n) for both
  • delete: O(1) for Set, O(n) for Array

...etc.

If by "lookups" you mean looking up by index, I'd note that the default Set implementation is unordered, so it doesn't support that operation in the same way an Array does.

like image 116
Greg Campbell Avatar answered Oct 19 '22 14:10

Greg Campbell