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