midrange.com code scratchpad |
Name:
getMinMaxSorta.rpgle.txt
|
Scriptlanguage:
Plain Text
|
Tabwidth:
4
|
Date:
04/24/2009 05:43:17 pm
|
IP:
Logged
|
|
Description:
Performance test to compare methods of finding the minimum and maximum values in an array. This method sorts the array, leaving the minimum value in the first element and the maximum value in the last element. The RPG code is elegant, but it performs poorly compared to other methods. See also getMinMaxLoop.rpgle.txt
|
Code:
- D arr1 s 10a dim(500)
- D arr2 s 10a dim(500)
- D i s 10i 0
- 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;
- arr2 = arr1;
-
- // sort the array. The minimum will be arr2(1)
- // and the maximum will be arr2(500)
- sorta arr2;
- 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';
-
|
|
|