What are the advantages of using Ruby NArray over Array?

2.1k views Asked by At

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/

3

There are 3 answers

0
masa16 On BEST ANSWER

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.
0
Jörg W Mittag On

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.

0
Nat On

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)