APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

The hidden loop

© September 2006 Anthony Lawrence


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:

::export Word stat=all.csv:
:f eq "":gosub header;f="1":
... (omitted code)

header::stat("1")="Date Entered"
::stat(2)="Order/Inv Number"
::stat(3)="Order Date"
... (omitted code)
::stat(318)="Xtra foo""

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.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> The hidden loop

Inexpensive and informative Apple related e-books:

El Capitan: A Take Control Crash Course

iOS 8: A Take Control Crash Course

Are Your Bits Flipped?

Take Control of OS X Server

Take Control of Upgrading to El Capitan

More Articles by © Anthony Lawrence

Printer Friendly Version

Have you tried Searching this site?

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.

Contact us

Printer Friendly Version

Technology is a word that describes something that doesn’t work yet. (Douglas Adams)

Linux posts

Troubleshooting posts

This post tagged:


Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode