简体   繁体   English

如何在Ruby中初始化哈希的大小?

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

I would like to specify an initial size for my Hash in Ruby. 我想为Ruby中的Hash指定初始大小。 I know I am going to be adding tens of millions of entries in the hash and a lot of time is wasting rehashing the data every time Ruby decides it needs a larger hash. 我知道我将在哈希中添加数千万个条目,并且每当Ruby决定需要更大的哈希时,大量的时间就浪费了重新哈希数据的时间。

Other languages support something like a "reserve" call to pre-size the hash. 其他语言支持诸如“保留”调用之类的功能,以对哈希进行预大小设置。 Can this be done in Ruby? 可以在Ruby中完成吗?

Ruby's Hash doesn't have any way to do that. Ruby的Hash没办法。 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. 无论哪种方式,它都应该仍然相对有效,但是对于这种规模的数据集,您可能需要考虑其他数据存储方法,例如数据库或键/值存储(例如Memcached或Redis)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM