Recent Articles

Webmin, Usermin Need Updates
Google's release of a code repository for open source projects has a number of languages represented...

Seeing Perl In Google Code
Google's release of a code repository for open source projects has a number of languages represented, including Perl.

Perl Plus Jifty Equals Hiveminder
Yes, Jifty is another web application builder, and yes, Hiveminder is another to-do list; but it is Perl that made them possible.

Perl Bootcamp Saddles Up For Germany
The next Big Nerd Ranch session of Perl Bootcamp occurs in Germany in September, and will cover Intermediate...

Perl Coders Get New GTK+ Release
Programmers on Perl and other languages can take advantage of the latest stable release of the GTK+...

Yet Another Perl Conference A Week Away
YAPC:NA 2006 takes place June 26th-28th in Chicago, with Larry Wall and Damian Conway providing the keynotes that will sandwich the conference's events...


09.19.06


The Hidden Loop

By A.P. 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:

::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.

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


About PerlProNews
PerlProNews is a collection of news and commentary designed to keep you in step with the ever evolving landscape of Perl environments. News and Advice for Perl Professionals

PerlProNews is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
PerlProNews.com SQLProNews.com
SysAdminNews DevWebPro.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITCertificationNews.com


-- PerlProNews
is an iEntry, Inc. publication --
iEntry, 2549 Richmond Rd. Lexington KY, 40509
2006 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article



Database Forum News and Advice for Perl Professionals PerlProNews News Archives About Us Feedback PerlProNews.com About Article Archive News Downloads WebProWorld Forums iEntry Advertise Contact Jayde