...: Marsh Chatter
Red Wing Hockey Tonight
Chicago Hospitality...
…hopefully doesn't mean handling the Wings, who have done very well this year against the Blackhawks, like last year. Looking forward to a good game after a nice one last night against Atlanta.
Hossa came through with 2 goals against his former team; one that made the ESPN highlight reel.
See you at the United Center…
Day of .NET - Ann Arbor

Yep, that's right. Back to A-squared this Saturday to meet and greet other developers and learn a few things. Expecting a lot of great presentations!
So many sessions, so little time. It would be great to be able to attend every session, alas tough choices had to be made.
I'm looking forward to all of the sessions I selected, however, two speakers in particular are covering topics which I am specifically trying to ramp up knowledge, they are: Michael Eaton presenting on the subject of what I call "CRUD alternatives" and Martin Shoemaker, the UMLGuy has more UML offerings.
Thank you to all the speakers and the sponsors. I certainly appreciate your efforts, which enable this event to be possible!
Be there AND be A-Squared.
Choices… A Recurring Series: For Loop vs. Do Loop…
Several years back, students in one of my Visual Basic classes, asked the question “which is better to use, a For Loop or a Do Loop”. At the time, I just rattled off the “book answer” which is the culmination of content I have read for many years in paper books and online. That answer is that the For Loop is faster. Of course, some of us do not find that a satisfying answer because going along with the "which" is the “how much or why” question.
For this "Choices..." article, I will answer the "which" & "how much" part of the question. To do that, I created test code samples that I used to evaluate the differences in processing time for two loop types; a For Loop and a Do Loop. Due to the load of running this type of process, I cannot make a live test available here (e.g. potential DoS attacks are highly probable), therefore the code pages will be available in ZIP format (here) for you to test or evaluate on your own system.
If you are interested in the "why" part of the question, you will have to seek out info from the "Level 1" programmers.
The following shows the "meat" (or for those vegans out there... the "fiber") of each loop used in this demo.
For Loop
' do the tested loop
StartTime = Date.Now
For TestedLoopCounter = 0 To MaxTestedValue Step 1
' overhead of loop only
Next
EndTime = Date.Now
Do Loop
' do the tested loop
StartTime = Date.Now
Do
' overhead of loop only
TestedLoopCounter += 1
Loop While TestedLoopCounter < MaxTestedValue
EndTime = Date.Now
Quick Thoughts
As noted, it’s very easy to say – just use a For Loop for all looping situations where you know the start & end points. In general, you cannot go wrong using a For Loop and I cannot imagine why someone would choose a slower method over a faster method; however, there are some people who prefer Do Loops. I don’t know why…, maybe it’s just what they learned.
Testing & Measurement Process
Each loop type was run "empty" for 100 million cycles by 10 times. From the data collected, I calculated averages, means, and standard deviations. I used 5 different computers and ran the tests multiple times to verify that the results were reasonably consistent.
The Results
Averaging across all 5 test systems: the average processing time of a...
- ... For Loop was 135 ms, with a low of 82 ms
- ... Do Loop was 335 ms, with a low of 252 ms
Conclusions
What do we know from these results?
- It is clear - the For Loop is faster than a Do Loop by a factor of at least 3. If the results were within a few milliseconds - a more scientific test would have to be undertaken. Clearly this is not needed.
- The For Loop has less overhead than a Do Loop.
- The slowest system & loop type processed 100 million cycles in about 1/3 of a second, while the fastest was less than a tenth of a second.
More Conclusions: 'er an editorial
Hello! For everyday programming work used by a few people - does it really matter? How many times a day do you process 100 million of anything? Arguing about this speed difference is like putting a big-block V8 into a VW Bug and then driving at 55 MPH - really worth the effort... right?
Now, if your code will be used by multiple people (e.g. say 100's or 1000's)... well, then, see the following...
So... How do you make a Loop Choice?
What is clear is that the bottleneck of a loop process is not the loop itself, it is the work that occurs within the loop. "Seasoned" programmers are right now saying "Doh"!
Choose the proper loop and then put your effort into optimizing the work. ProperLoop Choice? Simply...
- ... use the For Loop for all loop situations where you have, or can derive, a start and stop point before the loop begins.
- It is the easiest loop to use and for the most part protects you against creating an infinite loop.
- Yes, you can write code that causes a For Loop to go infinite - but you have to want to do it vs. unintentionally creating a malformed condition for a Do Loop.
- ... use the Do Loop for any loop situation where you do not know the stop point, other than it is more of a "condition". e.g. x < 10, y > 1000.
The Test Systems
The goal of this test was NOT what computer could run the loops the fastest. The goal was to find out if the For Loop was faster than a Do Loop and was it consistently faster. Therefore, I did not try to optimize the test systems. They are home & work machines used for varying tasks.
The test systems included 32-bit Windows XP and 64-bit Windows Vista operating systems. The hardware included 5 & 6 year old single processor systems and more recent dual core & quad core systems, hosting 1 GB to 6 GB of RAM. Additionally, the processing never resulted in page file access, which would have skewed the results wildly.
Caveats
Do not test on your own system and expect to derive a valid comparison of one computer over another. Testing computer hardware involves a much more intensive suite of tests than something as simple as an empty for/do loop block of code. Likewise, you should not convert this process to another language and then derive some comparison to the language I selected.
If you do either of the above... your comparisons, and any conclusions you make, will be wrong and completely unscientific.
What about the Code?
OK, I hear you. The example program is made up of 4 "files". They are:
- Cascading Style sheet (CSS) - Stylesheet.css (below the css folder) - this file contains very basic styles for the demo form.
- The ASP.NET web form - Default.aspx - this file contains controls (e.g. ASP.NET controls and xHTML "tags") used to design the data entry form and results area.
- The ASP.NET code-behind page - Default.aspx.vb - this file contains Visual Basic code to respond to the events on the web form and to process the loops.
- The Web.config - ASP.NET configuration page - this file is the generic config file created when creating an ASP.NET web site. I made 2 changes: 1) turned on Strict and 2) turned on Explicit -- see the <compilation> section. NOTE: do not turn on Debug - this will affect the results enough that you will not have consistent results.
The Downloads
Get it here.
This ZIP file contains a Microsoft Excel file (e.g. 2007 version) and a folder containing a Visual Studio 2005 web (e.g. VB.NET code).
"Quick start": use Visual Studio 2005 to open the folder as a FileWEB application, then run the Default.aspx file.
Alternate "starts"
- You could install the web folder on an IIS web server, set it up as an application and then run it from there.
- If you prefer other languages... you will find that the code is very readable and you could easily convert to your language of choice.
The next "Choices..." article will compare string concatenation vs. using String Builder object. Stop back soon.
Hockey Action Soon!
Only another 9-10 days now until we can see the Champs return to a new season.
Looking forward to some Ice Time. Good Luck Wings and Go for Back to Back Cups - like the 90's!
Kalamazoo also happens to be the home of the K Wings - IHL team. So we are doubly blessed with hockey action. Here's to another great season for our K Wings!
If you don't follow hockey - you are missing out. It's fast action, finesse, TEAM ACTION, and unbelievable twists & turns.