SciRuby / nmatrix

Dense and sparse linear algebra library for Ruby via SciRuby
Other
469 stars 133 forks source link

NMatrix getter and setter way slower than Matrix #541

Closed lokeshh closed 7 years ago

lokeshh commented 8 years ago

I'm using NMatrix for competitive programming for a while now and I have found NMatrix to be slower than Matrix for most of my programs. For example methods such as [], []= which are frequently used in my programs are slower in NMatrix.

Here's a benchmark I ran and following are the results:

       user     system      total        real
Matrix getter  3.700000   0.020000   3.720000 (  3.744560)
NMatrix getter 12.930000   0.060000  12.990000 ( 13.065677)
Matrix setter  2.920000   0.020000   2.940000 (  2.965361)
NMatrix setter 16.600000   0.060000  16.660000 ( 16.889194)
v0dro commented 8 years ago

Damn that sucks. Need some urgent performance fixes for nmatrix.

wlevine commented 8 years ago

@lokeshh Good catch! I think this is due to a lot of heap allocation of SLICE objects, which can be avoided. I will try to have a patch soon.

wlevine commented 8 years ago

For #[]=, I notice that we're spending a lot of time in nm::dense_storage::set(), allocating and freeing stuff. I'm not going to look into this any more at the moment, just noting this for future reference.

translunar commented 8 years ago

Thanks for looking into this, @wlevine. I concur with your explanation.