Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

As usual with performance problems the only way of knowing for sure is to use a profiler and check which piece of code is taking the longest time. However looking at the code you are copying the matrix element by element which is typically the slowest way to go. Often there is a method which can do that as a bulk operation for you.

I don't know much about the Matlab stuff as I've never used it but I found this question on Stackoverflow this question on Stackoverflow which has a couple of answers showing a single line statement of copying it. Based on one of these answers your code for the 2D case could look like this:

double[,] data = (double[,])matrix.ToArray(MWArrayComponent.Real);

I assume in the 3D case you'll get a double[,,] back which you might have to take apart afterwards so it fits your List<double[]> type.

Apart from that:

This exception has a misleading message in case the dimensions are less than 2

if (dimensions.Length > 3 || dimensions.Length < 2)
{
 throw new ArgumentException("Input matrix dimensions are too long", "Three is the max dimensions");
}

As usual with performance problems the only way of knowing for sure is to use a profiler and check which piece of code is taking the longest time. However looking at the code you are copying the matrix element by element which is typically the slowest way to go. Often there is a method which can do that as a bulk operation for you.

I don't know much about the Matlab stuff as I've never used it but I found this question on Stackoverflow which has a couple of answers showing a single line statement of copying it. Based on one of these answers your code for the 2D case could look like this:

double[,] data = (double[,])matrix.ToArray(MWArrayComponent.Real);

I assume in the 3D case you'll get a double[,,] back which you might have to take apart afterwards so it fits your List<double[]> type.

Apart from that:

This exception has a misleading message in case the dimensions are less than 2

if (dimensions.Length > 3 || dimensions.Length < 2)
{
 throw new ArgumentException("Input matrix dimensions are too long", "Three is the max dimensions");
}

As usual with performance problems the only way of knowing for sure is to use a profiler and check which piece of code is taking the longest time. However looking at the code you are copying the matrix element by element which is typically the slowest way to go. Often there is a method which can do that as a bulk operation for you.

I don't know much about the Matlab stuff as I've never used it but I found this question on Stackoverflow which has a couple of answers showing a single line statement of copying it. Based on one of these answers your code for the 2D case could look like this:

double[,] data = (double[,])matrix.ToArray(MWArrayComponent.Real);

I assume in the 3D case you'll get a double[,,] back which you might have to take apart afterwards so it fits your List<double[]> type.

Apart from that:

This exception has a misleading message in case the dimensions are less than 2

if (dimensions.Length > 3 || dimensions.Length < 2)
{
 throw new ArgumentException("Input matrix dimensions are too long", "Three is the max dimensions");
}
Source Link
ChrisWue
  • 20.6k
  • 4
  • 42
  • 107

As usual with performance problems the only way of knowing for sure is to use a profiler and check which piece of code is taking the longest time. However looking at the code you are copying the matrix element by element which is typically the slowest way to go. Often there is a method which can do that as a bulk operation for you.

I don't know much about the Matlab stuff as I've never used it but I found this question on Stackoverflow which has a couple of answers showing a single line statement of copying it. Based on one of these answers your code for the 2D case could look like this:

double[,] data = (double[,])matrix.ToArray(MWArrayComponent.Real);

I assume in the 3D case you'll get a double[,,] back which you might have to take apart afterwards so it fits your List<double[]> type.

Apart from that:

This exception has a misleading message in case the dimensions are less than 2

if (dimensions.Length > 3 || dimensions.Length < 2)
{
 throw new ArgumentException("Input matrix dimensions are too long", "Three is the max dimensions");
}
default

AltStyle によって変換されたページ (->オリジナル) /