prino
Senior Member
Joined: 07 Feb 2009 Posts: 1306 Location: Vilnius, Lithuania
|
|
|
|
I've got a bit of code that adds CHAR(4) variables (up to a maximum of around 200) as CHAR(5) (i.e appended with a blank) in alphabetic order to a CHAR (1022) VAR.
The code I now use is:
Code: |
dcl c char (4);
dcl cs char (1022) var;
dcl tcn char (5);
dcl #c fixed bin (31);
dcl #i fixed bin (31);
tcn = c;
if index(cs, c) = 0 then
do;
#c = #c + 1;
do #i = 1 to length(cs) by 5 while(substr(cs, #i, 4) < c);
end;
cs = substr(cs, 1, #i - 1) || tcn || substr(cs, #i);
end; |
but having been bitten by this use of index() before in a really, Really, REALLY bad way, the following seems to be far more efficient:
Code: |
tcn = c;
do #i = 1 to length(cs) by 5 while(substr(cs, #i, 5) < tcn);
end;
if substr(cs, #i, 5) ^= tcn then
do;
#c = #c + 1;
cs = substr(cs, 1, #i - 1) || tcn || substr(cs, #i);
end; |
but of course running this with the "COUNT/FLOW" option of the V2.3.0 OS Compiler leads to statement counts that are significantly higher due to the fact that the "do #i" loop in now always executed, be it that it's far more efficient than the old "index()", as that only checks on a character-by-character basis.
Does anyone have any smart method, without having to actually print assembler listings and count the actual number of assembler statements executed, to check the efficiency of both approaches. I'm sure doing this lots of times in a loop (I can provide the currently 4,321 actual values of "c" (33 different ones) that are pumped into the routine) and timing it with Strobe would undoubtedly provide a clear winner, but sadly I don't have access to Strobe. |
|