如何在Ruby中初始化哈希的大小? [英] How do I initialize the size of a Hash in Ruby?

查看:86
本文介绍了如何在Ruby中初始化哈希的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Ruby中的哈希指定初始大小.我知道我将在哈希中添加数千万个条目,并且每当Ruby决定需要更大的哈希时,很多时间都在浪费重新哈希数据的时间.

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 the data every time Ruby decides it needs a larger hash.

其他语言支持诸如"reserve"调用之类的功能,以对哈希值进行预大小设置.可以在Ruby中完成吗?

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

推荐答案

Ruby的Hash没办法.无论哪种方式,它的性能都应该相对较高,但是对于这种规模的数据集,您可能需要考虑其他数据存储方法,例如数据库或像Memcached或Redis这样的键/值存储.

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.

这篇关于如何在Ruby中初始化哈希的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆