midrange.com code scratchpad
Name:
DOW vs FOR loop performance test.
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
06/23/2011 12:57:19 pm
IP:
Logged
Description:
This test illustrates that the FOR loop is 3x slower to execute than a DOW loop.
Code:
  1.      dx...
  2.      d                 s             10i 0
  3.      dy...
  4.      d                 s             10i 0
  5.      dz...
  6.      d                 s             10i 0
  7.      diters...
  8.      d                 s             10i 0 inz(100000000)             
  9.  
  10.      d*****************TIMER VARIABLES****************
  11.      d starttime...
  12.      d                 s               z
  13.      d time_taken...
  14.      d                 s             15s 0
  15.      d timediff...
  16.      d                 s             40a
  17.      d************************************************         
  18.       /free
  19.         *inlr = *on;    
  20.        //**************************************************//
  21.        //Test how many iterations happen in For x downto 0
  22.        //and dow x <= y and see if the results are x or x+1
  23.        //
  24.        x = 0;
  25.        y = 0;
  26.        z = 0;
  27.        starttime = %timestamp();
  28.        for x = iters downto 1;
  29.        endfor;
  30.        time_taken = %diff(%timestamp():starttime:*MSECONDS);
  31.        timediff = 'FOR DOWNTO: ' + %char(time_taken);
  32.  
  33.        dsply timediff;
  34.  
  35.  
  36.        starttime = %timestamp();
  37.        for x = 1 to iters;
  38.        endfor;
  39.        time_taken = %diff(%timestamp():starttime:*MSECONDS);
  40.        timediff = 'FOR TO: ' + %char(time_taken);
  41.        dsply timediff;
  42.  
  43.        x = 1;
  44.        y = iters;
  45.  
  46.        starttime = %timestamp();
  47.        dow x <= y;
  48.          x+=1;
  49.        enddo;
  50.        time_taken = %diff(%timestamp():starttime:*MSECONDS);
  51.        timediff = 'DOW: ' + %char(time_taken);
  52.        dsply timediff;
  53.       /end-free      
  54.                                                                            
© 2004-2019 by midrange.com generated in 0.006s valid xhtml & css