Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of using Ruby NArray over Array?

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/

like image 557
Tilo Avatar asked Oct 20 '11 07:10

Tilo


1 Answers

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.
like image 74
masa16 Avatar answered Sep 25 '22 08:09

masa16