与 Array 相比,使用 Ruby NArray 有哪些优点?

发布于 2024-12-11 11:12:21 字数 736 浏览 4 评论 0原文

我刚刚遇到了 Ruby 的 NArray 库 —— 请原谅我在问这个问题时的无知:)

与标准 Ruby Array 实现相比,使用 NArray 库有哪些优势?

我已经看到 NArray 是面向数值计算的,但是看看 API,看起来好像只有一些针对数值的 Array 扩展——没有什么是你不能用 Array 做的。

  1. 为什么不直接使用大批?
  2. 是否有巨大的速度优势?
  3. 是否有巨大的内存优势?
  4. 与使用常规 Ruby Array 类相比,还有其他优点吗?

谷歌并没有真正对这个问题给出有用的解释。

我找到的参考文献:

http://rubydoc.info/gems/narray-ruby19/0.5 .9.7/NArray

http://narray.rubyforge.org/index.html.en

http://raa.ruby-lang.org/project/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..

  1. Why not just use Array?
  2. Is there a huge speed advantage?
  3. Is there a huge memory advantage?
  4. 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

http://narray.rubyforge.org/index.html.en

http://raa.ruby-lang.org/project/narray/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心碎无痕… 2024-12-18 11:12:21

另请参阅有关 NArray 的幻灯片:
http://www.slideshare.net/masa16tanaka/narray-and-scientific-computing-与红宝石

看起来只有一些对数组的扩展

No 的扩展,它与 Array 完全不同。
NArray 具有许多数值函数和多维特征。
另一方面,NArray 是静态的;它没有push/pop方法等。
NArray 的方法列表为 http://narray.rubyforge.org/SPEC.en

_1。为什么不直接使用数组呢?

数组保存 Ruby 对象。保存数值的效率很低。

_2。速度优势是否巨大?

是的。上述幻灯片的第 36 页显示 NArray 的速度提高了 50 倍。

请注意,如果循环是用 Ruby 编写的,则 Array 比 NArray 更快。

_3。是否有巨大的内存优势?

是的。至于 Float 值,在我的 64 位 Linux 机器上,Array 消耗的内存大约是 NArray 的 4 倍。

_4。与使用常规 Ruby Array 类相比,还有其他优点吗?

  • 支持多维数组
  • 支持数值函数
  • 无需对数组项进行垃圾回收。对于大型数组,GC 需要花费大量时间。
  • ETC。

See also the slide about NArray:
http://www.slideshare.net/masa16tanaka/narray-and-scientific-computing-with-ruby

it looks like there are only a few extensions over Array

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

_1. Why not just use Array?

Array holds Ruby Objects. It is inefficient to hold numerical values.

_2. Is there a huge speed advantage?

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.

_3. Is there a huge memory advantage?

Yes. As for Float values, Array consumes about 4 times more memory than NArray on my 64bit Linux machine.

_4. Any other advantages over using the regular Ruby Array class?

  • Support of multi-dimensional array
  • Support of Numerical functions
  • No need for garbage collection on Array items. GC takes large time for large Arrays.
  • etc.
公布 2024-12-18 11:12:21

我已经看到 NArray 是面向数值计算的,但是看看 API,看起来只有一些针对数值的 Array 扩展——没有什么是你不能用 Array 做的..< /p>

你错过了最重要的一点:NArray 不仅仅是数值处理的扩展,它也是限制。特别是

  • NArray 元素只能是固定大小的整数或浮点数
  • NArray 本身也是固定大小的,它们不能缩小或增长

一个实现NArray 可以利用这些限制来提供卓越的性能。

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..

You are missing the most important point: NArray is not just extended for numerical processing, it is also restricted. In particular

  • NArray elements can only be fixed-size integers or floats
  • NArrays themselves are also fixed-size, they cannot shrink or grow

An implementation of NArray can exploit those restrictions to provide superior performance.

亢潮 2024-12-18 11:12:21

对于大型类型数组创建,NArray 可能会更快,但对于小型数组创建(例如临时中间对象),Ruby Array 似乎更快。

基准代码:

require 'benchmark'

n1 = 1000000
n2 = 10000
Benchmark.bm do |x|
  x.report("NArray short float length 5:") { n1.times { NArray.sfloat(5) } }
  x.report("NArray long float length 5 :") { n1.times { NArray.float(5) } }
  x.report("NArray short int length 5  :") { n1.times { NArray.sint(5) } }
  x.report("NArray long int length 5   :") { n1.times { NArray.int(5) } }
  x.report("NArray object length 5     :") { n1.times { NArray.object(5) } }
  x.report("Ruby Array length 5        :") { n1.times { Array.new(5) } }  

  x.report("NArray short float length 10000:") { n2.times { NArray.sfloat(10000) } }
  x.report("NArray long float length 10000 :") { n2.times { NArray.float(10000) } }
  x.report("NArray short int length 10000  :") { n2.times { NArray.sint(10000) } }
  x.report("NArray long int length 10000   :") { n2.times { NArray.int(10000) } }
  x.report("NArray object length 10000     :") { n2.times { NArray.object(10000) } }
  x.report("Ruby Array length 10000        :") { n2.times { Array.new(10000) } }
end

结果:

                              user       system     total     real
NArray short float length 5:  0.740000   0.020000   0.760000 (  0.756466)
NArray long float length 5 :  0.770000   0.020000   0.790000 (  0.791446)
NArray short int length 5  :  0.750000   0.020000   0.770000 (  0.772591)
NArray long int length 5   :  0.760000   0.020000   0.780000 (  0.777375)
NArray object length 5     :  0.780000   0.020000   0.800000 (  0.801149)
Ruby Array length 5        :  0.450000   0.010000   0.460000 (  0.461501)    <====
NArray short float length 10000:  0.230000   0.050000   0.280000 (  0.281369)
NArray long float length 10000 :  0.430000   0.000000   0.430000 (  0.428917)
NArray short int length 10000  :  0.110000   0.010000   0.120000 (  0.113683)
NArray long int length 10000   :  0.230000   0.040000   0.270000 (  0.275942)
NArray object length 10000     :  0.460000   0.110000   0.570000 (  0.570558)
Ruby Array length 10000        :  0.440000   0.040000   0.480000 (  0.476690)

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:

require 'benchmark'

n1 = 1000000
n2 = 10000
Benchmark.bm do |x|
  x.report("NArray short float length 5:") { n1.times { NArray.sfloat(5) } }
  x.report("NArray long float length 5 :") { n1.times { NArray.float(5) } }
  x.report("NArray short int length 5  :") { n1.times { NArray.sint(5) } }
  x.report("NArray long int length 5   :") { n1.times { NArray.int(5) } }
  x.report("NArray object length 5     :") { n1.times { NArray.object(5) } }
  x.report("Ruby Array length 5        :") { n1.times { Array.new(5) } }  

  x.report("NArray short float length 10000:") { n2.times { NArray.sfloat(10000) } }
  x.report("NArray long float length 10000 :") { n2.times { NArray.float(10000) } }
  x.report("NArray short int length 10000  :") { n2.times { NArray.sint(10000) } }
  x.report("NArray long int length 10000   :") { n2.times { NArray.int(10000) } }
  x.report("NArray object length 10000     :") { n2.times { NArray.object(10000) } }
  x.report("Ruby Array length 10000        :") { n2.times { Array.new(10000) } }
end

Results:

                              user       system     total     real
NArray short float length 5:  0.740000   0.020000   0.760000 (  0.756466)
NArray long float length 5 :  0.770000   0.020000   0.790000 (  0.791446)
NArray short int length 5  :  0.750000   0.020000   0.770000 (  0.772591)
NArray long int length 5   :  0.760000   0.020000   0.780000 (  0.777375)
NArray object length 5     :  0.780000   0.020000   0.800000 (  0.801149)
Ruby Array length 5        :  0.450000   0.010000   0.460000 (  0.461501)    <====
NArray short float length 10000:  0.230000   0.050000   0.280000 (  0.281369)
NArray long float length 10000 :  0.430000   0.000000   0.430000 (  0.428917)
NArray short int length 10000  :  0.110000   0.010000   0.120000 (  0.113683)
NArray long int length 10000   :  0.230000   0.040000   0.270000 (  0.275942)
NArray object length 10000     :  0.460000   0.110000   0.570000 (  0.570558)
Ruby Array length 10000        :  0.440000   0.040000   0.480000 (  0.476690)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文