# Re: Premature Optimization

**URL:** <https://www.ruby-forum.com/t/re-premature-optimization/73656>\
**Category:** Ruby\
**Created:** [October 25, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/re-premature-optimization/73656 "2006-10-25T23:52:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 25, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/re-premature-optimization/73656/1 "2006-10-25T23:52:51Z")

</div>

From: Gavin K.

> However, I believe that’s a pure-ruby implementation of matrices and  
> vectors. A quick bit of Googling looks ike RNum[1] might be crazy fast  
> and do what you need. I’ve never used it. I’ve also never used  
> NArray[2], but it might do what you need.
> 
> [1] [http://rnum.rubyforge.org/](http://rnum.rubyforge.org/)  
> [2] [http://narray.rubyforge.org/](http://narray.rubyforge.org/)

So, I got NArray working on Windows with the One-Click Installer, and  
yes, it’s just a _bit_ faster than the pure-ruby  
Matrix:

require ‘narray’  
require ‘matrix’  
require ‘benchmark’

ITERATIONS = 4  
NUM\_VECTORS = 100  
VECTOR\_SIZE = 5000

Benchmark.bmbm{ |x|  
x.report( ‘Create Matrix’ ){  
ITERATIONS.times{  
@all = (1…NUM\_VECTORS).map{  
Vector[\*(1…VECTOR\_SIZE).map{ rand.round }]  
}  
}  
}  
x.report( ‘Add up Matrix’ ){  
ITERATIONS.times{  
@all.inject{ |v1,v2| v1 + v2 }  
}  
}

x.report( ‘Create NArray’ ){  
ITERATIONS.times{  
@all = (1…NUM\_VECTORS).map{  
NVector[\*(1…VECTOR\_SIZE).map{ rand.round }]  
}  
}  
}  
x.report( ‘Add up NArray’ ){  
ITERATIONS.times{  
@all.inject{ |v1,v2| v1 + v2 }  
}  
}  
}

Rehearsal -------------------------------------------------  
Create Matrix 4.703000 0.016000 4.719000 ( 4.719000)  
Add up Matrix 8.297000 0.016000 8.313000 ( 8.312000)  
Create NArray 4.016000 0.000000 4.016000 ( 4.032000)  
Add up NArray 0.031000 0.000000 0.031000 ( 0.031000)  
--------------------------------------- total: 17.079000sec

```
                user system total real

```

Create Matrix 3.703000 0.000000 3.703000 ( 3.704000)  
Add up Matrix 8.250000 0.000000 8.250000 ( 8.250000)  
Create NArray 4.156000 0.000000 4.156000 ( 4.156000)  
Add up NArray 0.032000 0.000000 0.032000 ( 0.031000)
