Quantcast

The Hidden Loop

Get the WebProNews Newsletter:


[ Business]

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.

*Originally published at APLawrence.com

Add to Del.icio.us | Digg | Yahoo! My Web | Furl

Bookmark WebProNews:

A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com

The Hidden Loop
Comments Off
Top Rated White Papers and Resources

Comments are closed.

  • Join for Access to Our Exclusive Web Tools
  • Sidebar Top
  • Sidebar Middle
  • Sign Up For The Free Newsletter
  • Sidebar Bottom