From d74f30cd6fb3184806fee561d7b0cd8b99c313f7 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 24 Nov 2012 10:18:34 +0100 Subject: [PATCH] Sparse: perf on setting and clearing sparse matrix fields --- .../Storage/SparseCompressedRowMatrixStorage.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs index 148c4841..7d6d63da 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs @@ -116,11 +116,8 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // Move all values (with an position larger than index) in the value array to the next position // move all values (with an position larger than index) in the columIndices array to the next position - for (var i = ValueCount - 1; i > index - 1; i--) - { - Values[i + 1] = Values[i]; - ColumnIndices[i + 1] = ColumnIndices[i]; - } + Array.Copy(Values, index, Values, index + 1, ValueCount - index); + Array.Copy(ColumnIndices, index, ColumnIndices, index + 1, ValueCount - index); // Add the value and the column index Values[index] = value; @@ -181,11 +178,8 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { // Move all values (with an position larger than index) in the value array to the previous position // move all values (with an position larger than index) in the columIndices array to the previous position - for (var i = itemIndex + 1; i < ValueCount; i++) - { - Values[i - 1] = Values[i]; - ColumnIndices[i - 1] = ColumnIndices[i]; - } + Array.Copy(Values, itemIndex + 1, Values, itemIndex, ValueCount - itemIndex - 1); + Array.Copy(ColumnIndices, itemIndex + 1, ColumnIndices, itemIndex, ValueCount - itemIndex - 1); // Decrease value in Row for (var i = row + 1; i < RowPointers.Length; i++)