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:
  1.      D arr1            s             10a   dim(500)
  2.      D arr2            s             10a   dim(500)
  3.      D i               s             10i 0
  4.      D before          s               z
  5.      D after           s               z
  6.      C     *entry        plist
  7.      C                   parm                    ntimes           15 5
  8.      C                   z-add     ntimes        times            10 0
  9.       /free
  10.           // setup some data in the array
  11.           for i = 1 to 500;
  12.              arr1(i) = %char(i);
  13.           endfor;
  14.           arr1(300) = 'first val';
  15.           arr1(301) = '9999 last';
  16.  
  17.           // get the starting time
  18.           before = %timestamp();
  19.  
  20.           // repeat the task as many times as the caller requested
  21.           for i = 1 to times;
  22.              arr2 = arr1;
  23.  
  24.              // sort the array.  The minimum will be arr2(1)
  25.              // and the maximum will be arr2(500)
  26.              sorta arr2;
  27.           endfor;
  28.  
  29.           // get the ending time
  30.           after = %timestamp();
  31.  
  32.           // display the before and after times and the difference
  33.           dsply before;
  34.           dsply after;
  35.           dsply (%char(%diff(after : before : *seconds)) + ' seconds');
  36.           *inlr = '1';
  37.  
© 2004-2019 by midrange.com generated in 0.005s valid xhtml & css