Which loop you put these things in doesn't really matter much because you're still doing the check once per item in the loop. I often try to keep these sorts of checking statements as separate as the rest of the code as possible so (1) the purposes of the loops are kept separate and (2) it's easier to comment out the debugging statements later if you want.
There is one very small difference. It's no big deal so if you're happy with that answer stop reading now. If you want a little extra info, read on.
-----
The Debug.Assert statement is removed from the program when you make a release build. In that case, it doesn't add any extra time to the program at all.
If you have a loop that contains only Debug.Assert statements, then the loop is empty (because the Debug.Assert statements have been removed) but it still exists so it takes a little extra time. My computer can run a loop through about 2.2 million loops per second so the extra time is trivial unless you have a really big loop.
In contrast, if you put the Debug.Assert statement inside a loop that you need to run whether or not the debugging statements are removed, then you don't need to execute that extra empty loop, and that will save you a tiny amount of time.
Because the difference is so small, I would use whichever technique makes the most intuitive sense and not worry about the extra millisecond one method will cost over the other.
|