Skip to main content
1-Visitor
December 1, 2018
Question

Nested Range Variables order of evaluation

  • December 1, 2018
  • 1 reply
  • 2830 views

If you have nested range variables, which loop gets executed fastest?

For example in the following the trace indicates the inner loop gets executed fastest, but if you swap the a and b lines, the outer loop loop now seems to be executed fastest???

 

1 reply

25-Diamond I
December 1, 2018

In this crappy forum you unfortunately can't just copy and paste pictures to your posts. To embed pictures you have to use the camera icon from the menu (which meas you have to save the picture to a file first)

Its not clear to me how you would time execution time by looking at the traces!?

Its also not clear what exactly you mean with  "swap the a and b lines" and which "loop" you would consider the "outer" one.

Do you think that one of the calculations below executes faster than the others?
Z4 and Z5 should be a little bit faster, I guess.

B.png

BTW, the above calculations can be speeded up significantly by allocating space for the array before the loops

1-Visitor
December 2, 2018

I cut & pasted the formula from Mathcad into the post and it showed it correctly, but when it showed me the review it was converted to an icon. Oh well...

 

I'm not trying to measure execution time here.

In the attached file in my post I consider the "b" loop as the inner one and the "a" loop as the outer. If you run this you get a trace like this:

a=0, b=0
a=0, b=1
a=0, b=2
a=0, b=3
a=1, b=0
a=1, b=1
a=1, b=2
a=1, b=3
a=2, b=0 .........

which indicates to me that the outer loop "a" is set to 0 and then the inner loop "b" is run from 0 to 3, then "a" is set to 1 and "b" again run from 0 to 3 and so on........

If you then swap the range lines i.e. change

a:=0..6

b:=0..3

to

b:=0..3

a:=0..6

then the trace print out does not change. This indicates to me that now the inner loop variable is set to 0 and the outer is run from 0 to 3, then then inner set to 1 and the outer run again from 0 to 3.

I expected that in every case the outer would be set to 0 and the inner run over its range, then the outer set to 1 and the inner run over its range again etc...  But it appears to be inconsistent in this.

So my question is how does Mathcad decide which loop it will hold constant while the other is run over its range??

Roger

 

25-Diamond I
December 2, 2018

You wrote "now seems to be executed fastest" and so I guessed you were talking about execution time.

 

So you really think that the order the two ranges a and b are defined has any influence on the way Mathcad loops over Z[a,b ? Why do you think so? Changing the order of the definitions of a and b does not has any effect on later calculations which use a and b.

The expression Z[a,b stands on its own when it comes to execution - when it sees the indices a and b it looks up their value and it does not matter at all where on the sheet those variables were defined (as long as its above or to the left). Z[a,b always evaluates with a as the outer "loop" simply because its the index placed first (row index).

Z[b,a evaluates the other way round and will of course give you the transposed matrix.