23-Emerald V
January 21, 2010
Question
While and For Loop Performance
- January 21, 2010
- 1 reply
- 1316 views
Can't remember if this has been discussed before ...
I was messing around with prime number generation and hit a problem with a for loop, in that the iteration range definition (first, next..last) could sometimes mean next was greater than last. The immediate solution was to simply rewrite the for loop as a while loop. However, I noted that the performance was considerably worse.
As a quick check on the relative performance of the two forms, I put together the attached worksheet. It shows that a for loop executes much quicker than a while loop in M11, M13 & M14. (M14 is quicker than M11, BTW).
Stuart
I was messing around with prime number generation and hit a problem with a for loop, in that the iteration range definition (first, next..last) could sometimes mean next was greater than last. The immediate solution was to simply rewrite the for loop as a while loop. However, I noted that the performance was considerably worse.
As a quick check on the relative performance of the two forms, I put together the attached worksheet. It shows that a for loop executes much quicker than a while loop in M11, M13 & M14. (M14 is quicker than M11, BTW).
Stuart

