# A simple For loop visibly slowing down. Advice to fix?

I have run into a unique problem. I am going to try and describe my code and what is happening as understandably as possible.

I have a For loop that does the same set of calculations over and over again for different sets of data. Always, on the tenth iteration, the loops seems to get stuck. Initially I thought the problem was an infinite loop, but that was not logical since iterations before this worked perfectly - and quickly. I also considered it may have been a bad data the iteration was on, so I took out that data at the tenth iteration, but the problem still happened. The function I wrote for the loop works fine up to the 9th and the 11th and forward iterations, but no matter the data the loop is slowing down in this spot.

I used Dynamic to observe the For iterators. It was a nested For loop. On other iterations the loop takes 1-2 seconds max, but on the problem it starts very quick, faster than I can see the Dynamic numbers move, but then it slows down to one iteration per second, then one iteration per two seconds, getting longer and longer. This problem is happening on the inner of the nested for loops, so you can imagine it would take forever to complete. It keeps getting progressively slower and slower.

So I have eliminated the possibility that it was bad data or improper code, because other iterations work great. Can anyone explain why this is happening and offer a way to fix it? I have the sample of my code were I have tracked the problem below. I think it is the Euclidean distance function that is getting slowed down. This is modified source code from Wolfram Demonstrations. score is a matrix whose first column and row are 3-D coordinate points. These points are my data.

For[i = 3, i <= (length2 + 2), i++,
For[j = 3, j <= (length1 + 2), j++,

diagonal =
score[[i - 1, j - 1]] +
If[EuclideanDistance[score[[1, j]], score[[i, 1]]] <= 2., match,
mismatch];
up = score[[i - 1, j]] + gap;
left = score[[i, j - 1]] + gap;
localMax = Max[diagonal, up, left];
If[type == "global" || localMax > 0,
score[[i, j]] = localMax;
arrows[[i, j]] = Switch[localMax,
diagonal, "\[UpperLeftArrow]",
up, "\[UpArrow]",
left, "\[LeftArrow]" ];
If[(type == "local") && (localMax >=
maxSoFar) && ((i >= maxCoords[[1]]) && (j >= maxCoords[[2]])),
maxSoFar = localMax; maxCoords = {i, j}]
]]]


If I can clarify anything please let me know. All assistance is appreciated!

CK

-
It will be easier to tell what's going on if you post a working example (code that we can just copy+paste and run) You can use RandomReal etc for the data, and remove as much code as possible to still retain the behavior. –  ssch Feb 17 at 1:23
"This is modified source code from Wolfram Demonstrations" Do Wolfram demonstrations really have stuff full of For loops? They are supposed to be vetted/reviewed/edited aren't they? –  Mike Honeychurch Feb 17 at 2:36
I tried to take it out of the function to debug it, but the slowing down doesn't happen then. I can't offer any runnable code since this error only is reproducible if the other 'stuff' is there. –  Chirese Feb 17 at 3:33
My best guess is that one of those comparisons/conditionals (including possibly the switch statement) is not evaluating strictly. e.g. If[a==b,1,2] remains unevaluated if either a or b is a pure symbol. Usually for conditional statements you want ===. General symbols like Max also don't strictly evaluate on symbolic input. –  amr Feb 17 at 4:34