Skip to main content
1-Visitor
February 27, 2013
Solved

Memory problems stacking Matrices

  • February 27, 2013
  • 2 replies
  • 14574 views

I have a small programm trying to split a matrix depending on the content in the rows. Now I'm using the folling algorithm:

take the row as submatrix on the actual position

stack that on one of the two alternative matrizes depending on a condition

Unfortunately this has a very poor performance both time and memory wise.

Assigning the rows via i,j to a new matrix seems to be better. Any ideas why?

thanks

Best answer by Werner_E

You didn't include the faster routine without using stack. But you are right, without stack the routine is MUCH faster! I don't know why - you would expect built-in routines to be more efficient than self written loops.

In the attached sheet you find 8 different split-routines. I have no idea how I could judge memory consumption other than watching process mathcad.exe in task manager which would be very unreliable, to say the least. So I have concentrated on time taking. In the pic you see the calculation times needed for splitting 10 times a 1000x1000 matrix.

The splitting routines:

split0: is your original program

split1: damp sqib; here I tried to replace the row selection from submatrix to a transpose/column/transpose combination and the routine went a lot slower (factor 10!). Transposing the whole matrix for every row obviously does cost a lot of time

split2: went back to submatrix but included some improvements (getting rid of if/oherwises, unnecessary submatrix,..) Does not do much better and in the last runs I could not duplicate the double speed of this rotine which I mentioned in the file.

split3: transpose the matrix and work column-wise instead of row-wise. That way we can use the column selector. Very slight improvement of calculation time

split4: The lonely winner! Unfortunately we are cheating here, as this routine does return nested vectors. To access a single number you would have to type M[ispace[0,j instead of M[i,j. So it does not count

split5: Trying to use split4 but "flattening" the result to get the desired matrix. flattening uses stack, so again big slow down.

split7: Using method of split4 and a flattening routine which replaces stack by for loops. More or less ex aequo with split6

split6: usually number one in time ranking (not counting split4). stack, augment, submatrix replaced by for loops. As you wrote this is much quicker (factor 10 to 12) - strange.

TestSplit1000.png

2 replies

25-Diamond I
February 27, 2013

Please, post a worksheet!

Efried1-VisitorAuthor
1-Visitor
February 27, 2013

ok here you are, data makes no sense- the programm collects all rows which have 0 in columns 0 and 1.

regards

Werner_E25-Diamond IAnswer
25-Diamond I
February 27, 2013

You didn't include the faster routine without using stack. But you are right, without stack the routine is MUCH faster! I don't know why - you would expect built-in routines to be more efficient than self written loops.

In the attached sheet you find 8 different split-routines. I have no idea how I could judge memory consumption other than watching process mathcad.exe in task manager which would be very unreliable, to say the least. So I have concentrated on time taking. In the pic you see the calculation times needed for splitting 10 times a 1000x1000 matrix.

The splitting routines:

split0: is your original program

split1: damp sqib; here I tried to replace the row selection from submatrix to a transpose/column/transpose combination and the routine went a lot slower (factor 10!). Transposing the whole matrix for every row obviously does cost a lot of time

split2: went back to submatrix but included some improvements (getting rid of if/oherwises, unnecessary submatrix,..) Does not do much better and in the last runs I could not duplicate the double speed of this rotine which I mentioned in the file.

split3: transpose the matrix and work column-wise instead of row-wise. That way we can use the column selector. Very slight improvement of calculation time

split4: The lonely winner! Unfortunately we are cheating here, as this routine does return nested vectors. To access a single number you would have to type M[ispace[0,j instead of M[i,j. So it does not count

split5: Trying to use split4 but "flattening" the result to get the desired matrix. flattening uses stack, so again big slow down.

split7: Using method of split4 and a flattening routine which replaces stack by for loops. More or less ex aequo with split6

split6: usually number one in time ranking (not counting split4). stack, augment, submatrix replaced by for loops. As you wrote this is much quicker (factor 10 to 12) - strange.

TestSplit1000.png

13-Aquamarine
May 26, 2017

I am noticing huge bottlenecks in Mathcad Prime 4 when splitting and flattening matrices when using the built in operator for row and built in function of stack.

If I explicitly redefine the operator and function as new functions I am seeing massive speed improvements. The speed is also affected by the type of value sent to a matrix. For instance, a string assigned to all elements of a 10000x4 matrix takes 32 sec to be split using the row operator. If I overwrite this it takes 0.094 seconds, no joke.

If I then flatten the resulting split matrix using the built in stack function, it takes 173 sec !!!!!!! vs 0.3 s if I do it explicitly.

What on earth is going on?

Also, if an integer is assigned to the matrix instead of a string the speed is massively reduced 0.011 secs vs 34 secs.

The reason for manipulating string matrices in this manner is of course to work with data sets.

Steve