Problem solve Get help with specific problems with your technologies, process and projects.

Faster LOOKUP on unsorted arrays

This tip provides information on Fast LOOKUP / LOKUP on unsorted arrays filled at run time.

Programs, especially print or other batch programs, often use an array or a set of arrays to store information from tables -- the data is retrieved from the arrays rather than using CHAIN every time a record is required.

For example, instead of always CHAINing to the COUNTRY code file every time you need to get the country name to print, load an array with the country codes and a parallel array with the country names. Now when your incoming data has a country code you LOOKUP the code in the code array and print the name from the corresponding element in the name array.

Everyone knows that the LOOKUP (or LOKUP) is faster than the CHAIN, and would use the method described above. But you can make it even faster, so that it can be used on tables, which contain very large numbers of records.

The first step is only to load an array element (and its parallels) when you need to. Don't load a large array with all the records from the table in *InzSr, but look up the entry in the array in the body of the program as normal; only if the entry is not found do you need to CHAIN the table and add the entry.

The second step is to load the arrays BACKWARDS: Start with the last element (say, element 4,000) and decrement the loading pointer to add each entry (so start the loading pointer at 4,001). To do a LOOKUP, set the reading pointer = the loading pointer and LOOKUP from that value. That way, LOOKUP is not wasting time searching all the array elements that haven't been filled yet!

This method works in RPGIII, RPGIV and RPG/400. For long-running batch programs that look up big tables it can speed up the run considerably, depending on what technique is currently used to access the table data -- I've halved run-time on some batch programs this way.


The Best Web Links: tips, tutorials and more.

Ask your programming questions--or help out your peers by answering them--in our live discussion forums.

Ask the Experts yourself: Our application development gurus are waiting to answer your programming questions.

Dig Deeper on iSeries CL programming

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.