与 Array 相比,使用 Ruby NArray 有哪些优点?
我刚刚遇到了 Ruby 的 NArray 库 —— 请原谅我在问这个问题时的无知:)
与标准 Ruby Array 实现相比,使用 NArray 库有哪些优势?
我已经看到 NArray 是面向数值计算的,但是看看 API,看起来好像只有一些针对数值的 Array 扩展——没有什么是你不能用 Array 做的。
- 为什么不直接使用大批?
- 是否有巨大的速度优势?
- 是否有巨大的内存优势?
- 与使用常规 Ruby Array 类相比,还有其他优点吗?
谷歌并没有真正对这个问题给出有用的解释。
我找到的参考文献:
http://rubydoc.info/gems/narray-ruby19/0.5 .9.7/NArray
I just came across the NArray library for Ruby -- please excuse my ignorance when asking this question :)
What are the advantages of using the NArray library over the standard Ruby Array implementation?
I've seen that NArray is geared towards numerical computing, but looking at the API, it looks like there are only a few extensions over Array geared towards numerical values -- nothing that you couldn't do with Array..
- Why not just use Array?
- Is there a huge speed advantage?
- Is there a huge memory advantage?
- Any other advantages over using the regular Ruby Array class?
Google didn't really come up with a useful explanation of this question.
References I found:
http://rubydoc.info/gems/narray-ruby19/0.5.9.7/NArray
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另请参阅有关 NArray 的幻灯片:
http://www.slideshare.net/masa16tanaka/narray-and-scientific-computing-与红宝石
No 的扩展,它与 Array 完全不同。
NArray 具有许多数值函数和多维特征。
另一方面,NArray 是静态的;它没有push/pop方法等。
NArray 的方法列表为 http://narray.rubyforge.org/SPEC.en
数组保存 Ruby 对象。保存数值的效率很低。
是的。上述幻灯片的第 36 页显示 NArray 的速度提高了 50 倍。
请注意,如果循环是用 Ruby 编写的,则 Array 比 NArray 更快。
是的。至于 Float 值,在我的 64 位 Linux 机器上,Array 消耗的内存大约是 NArray 的 4 倍。
See also the slide about NArray:
http://www.slideshare.net/masa16tanaka/narray-and-scientific-computing-with-ruby
No, it's completely different from Array.
NArray has many numerical functions and multi-dimensional features.
On the other hand, NArray is static; it does not have push/pop methods, etc.
NArray's method list is http://narray.rubyforge.org/SPEC.en
Array holds Ruby Objects. It is inefficient to hold numerical values.
Yes. p.36 of the above slide shows NArray is up to 50 times faster.
Note that Array is faster than NArray if the loop is written in Ruby.
Yes. As for Float values, Array consumes about 4 times more memory than NArray on my 64bit Linux machine.
你错过了最重要的一点:
NArray
不仅仅是数值处理的扩展,它也是限制。特别是NArray
元素只能是固定大小的整数或浮点数NArray
本身也是固定大小的,它们不能缩小或增长一个实现
NArray
可以利用这些限制来提供卓越的性能。You are missing the most important point:
NArray
is not just extended for numerical processing, it is also restricted. In particularNArray
elements can only be fixed-size integers or floatsNArray
s themselves are also fixed-size, they cannot shrink or growAn implementation of
NArray
can exploit those restrictions to provide superior performance.对于大型类型数组创建,NArray 可能会更快,但对于小型数组创建(例如临时中间对象),Ruby Array 似乎更快。
基准代码:
结果:
For large typed array creation NArray can be faster, though for small array creation (e.g. for temporary intermediate objects) Ruby Array seems to be fast is faster.
Benchmark code:
Results: