midrange.com code scratchpad |
Name:
getMinMaxLoop.rpgle.txt
|
Scriptlanguage:
Plain Text
|
Tabwidth:
4
|
Date:
04/24/2009 05:45:15 pm
|
IP:
Logged
|
|
Description:
Performance test to compare methods of finding the minimum and maximum values in an array. This method loops through the array searching for the minimum and maximum. This is probably the most efficient way of doing this. See also getMinMaxSorta.rpgle.txt
|
Code:
- D arr1 s 10a dim(500)
- D arr2 s 10a dim(500)
- D i s 10i 0
- D x s 10i 0
- D min s 10a
- D max s 10a
- D before s z
- D after s z
- C *entry plist
- C parm ntimes 15 5
- C z-add ntimes times 10 0
- /free
- // setup some data in the array
- for i = 1 to 500;
- arr1(i) = %char(i);
- endfor;
- arr1(300) = 'first val';
- arr1(301) = '9999 last';
-
- // get the starting time
- before = %timestamp();
-
- // repeat the task as many times as the caller requested
- for i = 1 to times;
- // get a copy of the original array
- arr2 = arr1;
-
- // find the minimum and maximum
- min = arr2(1);
- max = arr2(1);
- for x = 2 to 500;
- if arr2(x) > max;
- max = arr2(x);
- endif;
- if arr2(x) < min;
- min = arr2(x);
- endif;
- endfor;
- endfor;
-
- // get the ending time
- after = %timestamp();
-
- // display the before and after times and the difference
- dsply before;
- dsply after;
- dsply (%char(%diff(after : before : *seconds)) + ' seconds');
- *inlr = '1';
-
|
|
|