开发者

How do I initialize the size of a Hash in Ruby?

开发者 https://www.devze.com 2023-02-26 18:56 出处:网络
I would like to specify an initial size for my Hash in Ruby.I know I am going to be adding tens of millions of entries in the hash and a lot of time is wasting rehashing t开发者_运维问答he data every

I would like to specify an initial size for my Hash in Ruby. I know I am going to be adding tens of millions of entries in the hash and a lot of time is wasting rehashing t开发者_运维问答he data every time Ruby decides it needs a larger hash.

Other languages support something like a "reserve" call to pre-size the hash. Can this be done in Ruby?


Ruby's Hash doesn't have any way to do that. It should still be relatively performant either way, but you might want to consider other data storage methods, such as a database or key/value store like Memcached or Redis, for data sets of that magnitude.

0

精彩评论

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