Programmers use loops to avoid writing repetitive code, but sometimes forget that compilers will unroll their loops when possible for efficiency. You can unroll your own loops, too. Rather than testing and branching, the code is sequential: (pseudocode) "for (x=1;x<5;x++);print x;next" becomes "print '1 2 3 4 5'.
Of course looping code is almost always quicker to write and easier to understand and modify in the future, but it's not ideal for efficiency. Testing and branching takes time; if you need flat out performance, you avoid loops where possible.
But that's not the only reason to unroll a loop. Yesterday I needed to write a Filepro processing table to export a CSV dump of all the database fields. I could have written that with a loop, but instead I ended up with code that looks like this:
::f(1,*,g);no(3,.0):
::export Word stat=all.csv:
:f eq "":gosub header;f="1":
::stat(1)=1
::stat(2)=2
::stat(3)=3
... (omitted code)
::stat(318)=318
::end
header::stat("1")="Date Entered"
::stat(2)="Order/Inv Number"
::stat(3)="Order Date"
... (omitted code)
::stat(318)="Xtra foo""
::write;return
Even if you don't grok Filepro's strange looking code, you can probably get the concept here. I'm apparently doing a lot of hard work that could more easily be done in a loop, right?
Well, yes, but not in this case. And it actually was done in a loop.
That is, I didn't hand write all this. I created it with a Perl script that read the database "map" file to get field names, and that of course used a loop. So creating this code was simple and easy. But why do it that way? Surely performance wasn't the issue?
No, of course not. The issue was that a non-programmer needs to modify this code. He needs to take out fields he doesn't want, relabel some fields to make more sense to the ultimate consumer, and possibly rearrange the order of some fields for the same reason. Programming that in a loop is possible, but it gets messy very quickly. By giving him individual lines, he can pick and choose what he wants easily.
Looping is powerful stuff, but isn't aways the right method.
More Articles by Anthony Lawrence - Find me on Google+
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar