Here are some tables which INITIALIZE may be commonly be used on:
Code: Select all
01 2210M-ACCOUNT-MASTER-RECORD.
05 2210M-ACCOUNT-NUMBER PIC X(8)
.... another 43 items like that, including some packed-decimal fields, binary fields.
Code: Select all
01 GRAND-TOTALS.
an OCCURS with some packed-decimal fields.
Code: Select all
01 GRAND-TOTALS.
an OCCURS with some packed-decimal fields.
Code: Select all
01 TOTALS-BY-ACCOUNT.
an OCCURS with some packed-decimal fields.
INITIALIZE seems to have somehow gained the reputation of being "magic". "My program is working, I use INITIALIZE". A lot of processing is just plain wasted on INITIALIZE.
Take the 2210M-ACCOUNT-MASTER-RECORD. People will routinely INITIALIZE an 01-level and then immediately have 43 MOVE statements to put content in each field subordinate to the 01. Which means the entire INITIALIZE was nothing but waste. Even MOVE SPACE or MOVE ZERO to the 01-level is faster, but wasted. INITIALIZE for a simple 01-level is just a waste. Even if one field is missed in the MOVE, it is hard to spot, because it has a "valid" value. It takes fewer than three seconds to check on the compile listing that each field is amended on a record-layout. Why just be sloppy and wasteful and use INITIALIZE all the time for that?
Next we have GRAND-TOTALS. Since this only needs an initial value once, it doesn't much matter how you do it. The most effective way to do it is the VALUE clause (these days it can be on the items with, or subordinate to, and OCCURS, in olden-times, that was not possible). But it doesn't matter how you do it, as it is done once.
Next, GRAND-TOTALS-BY-MONTH. Only going to be initialised 12 times, but I'd do it the way I'd do it for 100,000 times, since it is code that is likely to be copied.
Now, TOTALS-BY-ACCOUNT. That's going to be 100,000 times.
Originally INITIALIZE would work as a loop, the compiler would generate a loop, terminated by the number of the OCCURS, and would do individual moves of the data.
Over the years, INITIALIZE has got faster, but it is nowhere near the most efficient way to do it But INITIALIZE can be part of the most efficient way to do it (when it needs to be done).
Most efficient way is with, at the "start" of a program, storing the table in its initial state (for which INITIALIZE can be used, or VALUE, or pretty much any way, as it will only be done once) and then copying that initial data to a second table. When the point in the program arises to initialise the table, you move that copy (the initialised one) back to the original source.
In olden times, storage available for the WORKING-STORAGE was considerably tighter (one megabyte) so we tended not to duplicate the tables. The "offset-MOVE" is a slightly slower way, faster than INITIALIZE, and requiring no (or not much, depending on implementation) additional storage.
After all that, remember that setting anything to an initial value is a waste of time if it is immediately going to be set to something else without reference to its current content.
A table where data external to the table is simply MOVEd to it does not need to be initialised. A table of counts or cumulative figures does need to be initialised if/when you get a "control break".