View previous topic :: View next topic
|
Author |
Message |
venuhunev
New User
Joined: 26 May 2007 Posts: 70 Location: chennai
|
|
|
|
Hi,
I have a program say Program XYZ will be called numerous times in production from main program say program ABC.
In sub program XYZ, to increase performance, Values are selected from DB2 database tables and stored in an internal program array.
I have a requirement where the array should be only populated once. That is, logic in the program should be such that it detects whether the values have already been populated in the array and should remain static each time the program is called.
How to do in PL/1 ?
Kindly Help. Thanks. |
|
Back to top |
|
|
prino
Senior Member
Joined: 07 Feb 2009 Posts: 1314 Location: Vilnius, Lithuania
|
|
|
|
Code: |
abc: proc options(main) reorder;
dcl array (.....);
dcl xyz entry;
call xyz(array);
do i = 1 to 10000000;
/* use values from array */
end;
end abc; |
Sheesh, was that so difficult? |
|
Back to top |
|
|
venuhunev
New User
Joined: 26 May 2007 Posts: 70 Location: chennai
|
|
|
|
Hi Prino,
Thnx for your reply.
Your code shows that xyz is called once and array value is fetched and used only once in main program.
But my question is, ABC calls XYZ in loop and Array is populated from DB2 tables and used in XYZ It should not be populated in all loops, only once inside the sub program. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10886 Location: italy
|
|
|
|
to clarify Robert post
since we do not know the details of the subroutine, You were given a general hint...
split the subroutine in two parts
part one to be called outside of the loop to initialize the array,
inside the loop just use the array already filled ( Robert snippet)
or call the subroutine doing the process-only part! |
|
Back to top |
|
|
prino
Senior Member
Joined: 07 Feb 2009 Posts: 1314 Location: Vilnius, Lithuania
|
|
|
|
Your requirements are as clear as mud, not surprisingly... My solution matches them.
Can you tell me why my solution does not match your requirements as given in the first post of the thread? |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10886 Location: italy
|
|
|
|
the TS is confused... he seems to believe that the i is the index of the array ! |
|
Back to top |
|
|
venuhunev
New User
Joined: 26 May 2007 Posts: 70 Location: chennai
|
|
|
|
ok.
I ll make my requirements very clear.
Code: |
abc: proc options(main) reorder;
dcl array (.....);
dcl xyz entry;
do i = 1 to 10000000;
call xyz(array);
end;
end abc; |
Code: |
xyz: proc
step1 - declare the array
step2 - populate the array only once frm the respective db2 tables.
step3 - perform other operations
step4 - return
end xyz; |
Step 2 should be executed only once even if ABC calls XYZ 1000000 times. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
regardless of programming language:
step two (2) should in addition to populating the array, set a flag.
then subsequent calls to the module would check the flag, and if set, skip step2.
you can declare a flag, initialized to 'no' or 'off' or 'notset'.-
end of step2, set the flag to 'yes', 'on', 'set'.
if you can't make that work, you can't keep the table populated between calls. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10886 Location: italy
|
|
|
|
Quote: |
Step 2 should be executed only once even if ABC calls XYZ 1000000 times. |
that' s what we kept telling You, call the subroutine to fill the array once before entering the <outer> loop
if You cannot understand such a simple suggestion You do not have a plece in IT |
|
Back to top |
|
|
don.leahy
Active Member
Joined: 06 Jul 2010 Posts: 765 Location: Whitby, ON, Canada
|
|
|
|
I am not a PLI programmer, but my favourite way of approaching this problem is to code the subroutine to behave as follows:
Code: |
Search array.
If not found
fetch from DB2
store value in array
endif
Return value from array.
|
This avoids having to load the entire array the first time the subroutine is called. I have often found that many processes use only a small percentage of the possible array elements, so you might as well only load the ones actually needed. Your mileage will vary. |
|
Back to top |
|
|
prino
Senior Member
Joined: 07 Feb 2009 Posts: 1314 Location: Vilnius, Lithuania
|
|
|
|
And by keeping the array "small", you can improve the efficiency even more by using a simple LRU algorithm to keep the most used values in it. (With "small" empirically defined based on the cost of going to DB2 vs the cost of looping through the array)
In PL/I you could conceivably even sort the array in a separate thread and use a binary search on it, although that might increase CPU use too much... |
|
Back to top |
|
|
Ronald Burr
Active User
Joined: 22 Oct 2009 Posts: 293 Location: U.S.A.
|
|
|
|
So much depends on the specifics:
The TYPE of table lookup (direct vs binary vs sequential) can be influenced by a number of factors, including:
1) the class of the table argument (numeric, alphanumeric) and
2) the range of possible argument values
For example: if the arguments are numeric and the range is from 1 to 1000, it may make more sense to store and access table entries using the argument itself as a subscript into the table.
3) the frequency distribution of argument
For example: if the argument is state-code and you are processing payroll data, it may make sense to populate the table in descending order by the number of employees in each state and perform a sequential search.
4) the maximum number of arguments possible vs
5) the average number of arguments accessed
For example: if there are 5 million possible argument values, but the average number accessed in a given run is only 5,000, then it may make sense to create a "sparse" array, in which only actually used arguments are stored (for future reference) rather than all 5 million. In some cases, it may make sense to "pre-scan" the input to normalize the actual argument values that will be used, and build the lookup table for just those arguments.
6) The order of the input arguments
For example, if the input is pre-sorted in ascending argument value, then it may make sense to not have a TABLE at all - as each search is initiated check to see if the argument matches a stored entry - if it does, you already have the result; if it has changed, retrieve the appropriate result, and save the argument and result for the next search. |
|
Back to top |
|
|
prino
Senior Member
Joined: 07 Feb 2009 Posts: 1314 Location: Vilnius, Lithuania
|
|
|
|
Even a mixed approach is possible when the distribution is extremely skewed, as, for example here in Belgium, with members of Belgian health insurers. About 97% of their members are Belgian, 1% are Dutch and French and the rest is the rest. By doing a linear search on B/NL/F followed by a binary search on the rest, processing was sped up enormously.
The old situation? Would you believe it? An unsorted array, with B somewhere past the middle... |
|
Back to top |
|
|
don.leahy
Active Member
Joined: 06 Jul 2010 Posts: 765 Location: Whitby, ON, Canada
|
|
|
|
Prino, Ronald, thanks for rescuing a thread that wasn't going anywhere. Sometimes even a "dumb" question can lead to an interesting discussion. |
|
Back to top |
|
|
PeterHolland
Global Moderator
Joined: 27 Oct 2009 Posts: 2481 Location: Netherlands, Amstelveen
|
|
|
|
Belgium is a not existing country. Half of it is french, half of it is dutch.
So how about distribution? |
|
Back to top |
|
|
|