Skip to main content
Werner_E
25-Diamond I
March 24, 2013
Solved

Is there an explanation for that huge speed difference?

  • March 24, 2013
  • 5 replies
  • 1920 views

Attached find a worksheet with three different routines to draw a circular frame around an RGB image.

I have no explanation why the second and third routine are over 100 times slower than the first, which uses an auxiliary routine which is called three times.

I would have expected FrameImg2 to be a bit more speedy than the other two because the if condition is only evaluated once and the i-loop is only triggered when necessary.

Anybody an explanation?

FrameImg.png

Best answer by RichardJ

Because in 2 and 3 when you loop through all the rows and columns you are indexing into a nested array every time. It is much faster to split out the 2D array three times and just index into those arrays.

5 replies

1-Visitor
March 25, 2013

I don't know why there is green area below in the Img2.

Werner_E
Werner_E25-Diamond IAuthor
25-Diamond I
March 25, 2013

zhu laojianke wrote:

I don't know why there is green area below in the Img2.

Right Click/Properties/Highlight Region/Choose Color and choosing the same color as was the second parameter given to FrameImg1/2/3.

RichardJ19-TanzaniteAnswer
19-Tanzanite
March 25, 2013

Because in 2 and 3 when you loop through all the rows and columns you are indexing into a nested array every time. It is much faster to split out the 2D array three times and just index into those arrays.

Werner_E
Werner_E25-Diamond IAuthor
25-Diamond I
March 25, 2013

Thanks for your explanation, Richard.

Is there any reason known, why indexing a nested array is sooo much slower? I can't think of one other than Mathcad maybe copies the indexed array to change a single value and copy it back afterwards, which would be a really unnecessary awkward way to implement that feature.

But I guess as we don't have access to the source code we can only accept things as they are. Similarily the incomprehensible effect that stack and augment are much slower than self written routines using nested for loops, as it was discussed in a former thread.

Thanks again for your help!

Werner

19-Tanzanite
March 25, 2013

It doesn't make much sense to me either. But, as you say, we have no way to know how it is implemented in the bowels of Mathcad.