开发者

Efficiencies of set operations vs array operations in ruby

开发者 https://www.devze.com 2023-02-13 15:48 出处:网络
What are the differences in efficiencies between set and array for operations? Examples: lookups iterations

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

Examples:


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消