IBM i > DEVELOPER > RPG

Look Before You %Lookup


Among the great new features in the V5R1 release of RPG IV are %Lookup and friends. At first glance, you might view these new built-in functions (BIFs) as simple replacements for the LOOKUP op-code. If you think these BIFs merely perform lookups in free-form without using the dreaded *IN indicators, you've failed to spot two of the great improvements incorporated by the compiler writers.

For instance, you can now specify the maximum number of entries to be searched, not just the starting index. This is useful when the array isn't full, because it avoids using obscure techniques such as filling the array from the back and limiting the search by specifying a starting index.

Unlike their ancestors, the new BIFs perform a binary search if the array is sequenced. In fact, a binary search such as this relies on the fact that the elements are in sequence. (Remember this as its important as our tale unfolds.) This can make an enormous performance difference when searching large (and even not so large) arrays. If you're unfamiliar with binary searches, a brief explanation may be in order.

Suppose you have an array of 150 elements and that the one you're searching for is in the 73rd position. The %Lookup BIF first selects the middle entry from the array (i.e., index 75) and then checks to see if the entry at that position is higher or lower than the one sought. In this case, its lower, so next the BIF checks index 37, the mid point of the remaining range (1 - 74). The entry found will be too low, further narrowing the range (38-74), so we check the entry at position 56, which is also too low. And so on until arriving at 73 (73 - 74) after a total of seven comparisons. That's an improvement over the LOOKUP op-code, which would have taken 73 comparisons to find the target. As you can see, even if we doubled the arrays size, %Lookup would only need one additional comparison. With the old LOOKUP, doubling the array size from 150 to 300 would (on average) have added an additional 75 comparisons.

So, why does the headline of this column sound a cautionary note? What exactly must we look out for? Surely if %Lookup is so much better, then using it in place of LOOKUP should be a no brainer, right? Not quite. Consider the following situation:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.


comments powered by Disqus

Advertisement

Advertisement

2017 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

New and Improved XML-INTO

Namespace support makes the opcode a viable option

Authenticating on the Web

The finer points of OpenRPGUI, Part 1

The Microphone is Open

Add your voice: Should IBM i include open-source RPG tools?

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters