出于性能和内部数据结构的原因,从出生开始,我就一直被教导要避免像瘟疫一样的嵌套数组。因此,我试图在 Ruby 中找到优化多维数据结构的良好解决方案。
典型的解决方案可能涉及使用一维数组并通过 x*width + y
访问每个数组。
Ruby 有能力重载 []
运算符,所以也许一个好的解决方案是使用 multi_dimensional_array[2,4]
甚至使用 splat 来支持任意维数。 (但实际上,我只需要两个维度)
是否已经有相关的库/gem?如果不是,那么编写这篇文章的最佳方式是什么?
我的嵌套数组查找是我计算密集型脚本的瓶颈,所以这是很重要的事情,而不是过早优化的情况。
如果有帮助,我的脚本主要使用随机查找和较少的遍历。
最佳答案
narray
NArray is an Numerical N-dimensional Array class. Supported element types are 1/2/4-byte Integer, single/double-precision Real/Complex, and Ruby Object. This extension library incorporates fast calculation and easy manipulation of large numerical arrays into the Ruby language. NArray has features similar to NumPy, but NArray has vector and matrix subclasses.
关于ruby - 在 Ruby 中优化 "Multidimensional"数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3672802/