While and For Loop Performance
Jan 21, 2010
03:00 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Jan 21, 2010
03:00 AM
While and For Loop Performance
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
Labels:
- Labels:
-
Other
1 REPLY 1
Jan 21, 2010
03:00 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Jan 21, 2010
03:00 AM
I think that this what you want see:

This into winxp sp3 with mathcad 11 eating 23460 Kb, as report task manager.
I have the following rule: "if the iterating number is knowed, then must to be a for loop ... ". Next is the exception: .. except that you can vectorize."
There is a thing that I not understand: can't you check which is first,next...last before the for loop?
You testing file coul be more interesting if you put it in the "real" math world. Here an interesting example:
Try to write the usual procedure for the bisection method for the zero of a monodic function, i.e. using while abs(b-a)>TOL and then try to write one other using a for loop. Notice that the iteration number is given for this algorithm.
Then call the zero's routines for a lot of functions (probably a parametric family to not write dozens of code pages) or call over the same function a lot of times to find the same zero to amplify the bussy time.
That's as suggestion.
Regards. Alvaro.

This into winxp sp3 with mathcad 11 eating 23460 Kb, as report task manager.
I have the following rule: "if the iterating number is knowed, then must to be a for loop ... ". Next is the exception: .. except that you can vectorize."
There is a thing that I not understand: can't you check which is first,next...last before the for loop?
You testing file coul be more interesting if you put it in the "real" math world. Here an interesting example:
Try to write the usual procedure for the bisection method for the zero of a monodic function, i.e. using while abs(b-a)>TOL and then try to write one other using a for loop. Notice that the iteration number is given for this algorithm.
Then call the zero's routines for a lot of functions (probably a parametric family to not write dozens of code pages) or call over the same function a lot of times to find the same zero to amplify the bussy time.
That's as suggestion.
Regards. Alvaro.
