What are the benefits of using Ruby NArray over an array?

I just stumbled upon the NArray library for Ruby - please excuse my ignorance by asking this question :)

What are the benefits of using the NArray library over the standard Ruby Array implementation?

I saw that NArray is numerical oriented, but looking at the API it seems that only a few extensions above the array are numerical oriented - nothing you could do with Array ..

  • Why not just use Array?
  • Is there a speed advantage?
  • Is there a huge memory advantage?
  • Any other advantages over using the regular Ruby Array class?

Google really did not come up with a useful explanation for this issue.

Links 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/

+7
arrays ruby numerical-computing narray
source share
3 answers

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

it looks like there are only a few extensions over the array

No, it is completely different from an array. NArray has many numerical functions and multidimensional functions. NArray, on the other hand, is static; it does not have push / pop methods, etc. NArray Method List http://narray.rubyforge.org/SPEC.en

_one. Why not just use Array?

The array contains Ruby objects. This is inefficient for storing numerical values.

_2. Is there a huge speed advantage?

Yes. p. 36 of the above slide shows that NArray is 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 the Float values, Array consumes about 4 times as much memory as NArray on my 64-bit Linux machine.

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

  • Multidimensional array support
  • Numeric Function Support
  • There is no need for garbage collection in Array elements. GC takes a lot of time for large arrays.
  • and etc.
+10
source share

I saw that NArray is numerical oriented, but looking at the API it seems that only a few extensions above the array are numerical oriented - nothing you could do with Array ..

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

  • NArray Elements can only be fixed-size integers or boards
  • NArray themselves are also fixed; they cannot shrink or grow.

A NArray implementation may use these limitations to provide superior performance.

+4
source share

For creating a large typed array, NArray can be faster, although for creating small arrays (for example, for temporary intermediate objects) Ruby Array seems to be faster faster.

Checkpoint 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) 
+1
source share

All Articles