home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / turbopas / sys60a.zip / RATE60A.PAS < prev    next >
Pascal/Delphi Source File  |  1991-03-26  |  6KB  |  149 lines

  1. { ========================================================================== }
  2. { Rate60a.pas - Rate timer using only low memory clock.   ver 6.0a, 03-26-91 }
  3. {                                                                            }
  4. { This rate timer is a highly accurate timer based on the low memory clock.  }
  5. { It is device independent by not accessing any special timer hardware and   }
  6. { does not use any interrupts.  Accuracy is +-1 repetition.                  }
  7. {                                                                            }
  8. { This timer is great for timing short events (1 microsecond to .1 second)   }
  9. { to get a Reps-per-second result.  The secret to such great accuracy is     }
  10. { keying in on the fact that the low memory clock is very accurate and on    }
  11. { schedule every time it ticks.  So, just make the event work by the clock   }
  12. { rather than vice-versa.                                                    }
  13. {                                                                            }
  14. { The timer is set for a 1 second test.  If you can't get enough repetitions }
  15. { (>100) to get any resolution, you are free to change TestTime up to 3600   }
  16. { seconds.                                                                   }
  17. {                                                                            }
  18. { The timer even accounts for the overhead of the timer itself and any other }
  19. { overhead that is a part of your event.  Just insert your code and go.  You }
  20. { may experience a little jitter while running in the integrated environment,}
  21. { but is solid when run as an executable file.                               }
  22. {                                                                            }
  23. { For further explanation of the high-resolution feature of this timer, see  }
  24. { comments at the end of this file.                                          }
  25. {                                                                            }
  26. {   Public Domain by James H. LeMay, Eagle Performance Software              }
  27. { ========================================================================== }
  28. {$A+,D+,E-,L-,N-,R-,S-,V-}
  29.  
  30. program RateTimer;
  31.  
  32. uses
  33.   Crt;
  34.  
  35. const
  36.   TestTime = 1;  { seconds }
  37.   TicksPerDay = 1573040.0;            { DOS timer ticks/day. }
  38.   TicksPerSec = TicksPerDay/86400.0;
  39.  
  40. var
  41.   LowClock: word absolute $0000:$046C;
  42.   Tick1,Ticks:           word;
  43.   Reps,OverheadReps:     longint;
  44.   RepsPerSec,TickFactor: real;
  45.   R: real absolute RepsPerSec; { Variable name is easier to Watch }
  46.   { -- Place your variables here: -- }
  47.   S1,S2: string;
  48.   b:     byte;
  49.   L1,L2,L3: longint;
  50.  
  51. procedure WaitForTick;
  52. begin
  53.   Tick1 := LowClock;
  54.   repeat
  55.   until LowClock<>Tick1;
  56.   Tick1 := LowClock;
  57. end;
  58.  
  59. procedure RunTime (Seconds: word);
  60. begin
  61.   if Seconds>3600
  62.     then Seconds:=3600;
  63.   Ticks := trunc(Seconds*TicksPerSec);
  64.   TickFactor := Seconds*TicksPerSec/Ticks; { Accounts for trunc of seconds }
  65.   WaitForTick;
  66. end;
  67.  
  68. procedure Init;
  69. begin
  70.   { ** Initialize any of your variables here: ** }
  71.   S1 := 'Now is the time to check out Eagle Performance Software Products '+
  72.         'for speed tests';
  73.   S2 := S1;
  74.   L1 := 12345678;
  75.   L2 := 100;
  76. end;
  77.  
  78. procedure ResetVariables;
  79. begin
  80.   { ** Place any variables that need to be reset after each test here: ** }
  81.   { StrMove (S2,S1); }
  82. end;
  83.  
  84. procedure Test;
  85. begin
  86.   Reps := 0;
  87.   RunTime (TestTime);
  88.   repeat
  89.     { ** Insert your test routine here ** }
  90.     L3 := L1 div L2;
  91.     { ** End of test routine ** }
  92.     ResetVariables;
  93.     inc (Reps);
  94.   until LowClock-Tick1>=Ticks;
  95. end;
  96.  
  97. procedure OverHead;
  98. begin
  99.   OverHeadReps := 0;
  100.   RunTime (TestTime);
  101.   repeat
  102.     ResetVariables;
  103.     inc (OverHeadReps);
  104.   until LowClock-Tick1>=Ticks;
  105. end;
  106.  
  107. procedure CalcRate;
  108. begin
  109.   RepsPerSec := TickFactor/((TestTime)*(1.0/Reps-1.0/OverHeadReps));
  110.   WriteLn ('Strings/sec = ',RepsPerSec:8:0);
  111. end;
  112.  
  113. begin
  114.   Init;
  115.   Test;
  116.   OverHead;
  117.   CalcRate;
  118. end.
  119.  
  120. { ========================================================================== }
  121. { The simplicity of the rate timer is deceptive.  Don't let it fool you.  If }
  122. { you take a just a cursory look, you may think that you can only get 1/18.2 }
  123. { second resolution out this timer.  But think again.                        }
  124. {                                                                            }
  125. { There are two clocks in your computer - (1) the 8253/8254 timer chip, and  }
  126. { (2) the CPU.  (One doesn't usually think of the CPU as a clock!)  Remember,}
  127. { the low-memory output for the clock comes from the 8253/8254 chip which is }
  128. { accurate to 1-microsecond, even though it doesn't "tick" but every         }
  129. { 1/18.2... seconds.                                                         }
  130. {                                                                            }
  131. { Since we can't get high resolution from the low-memory clock, the program  }
  132. { does the reverse and gets the high resolution from the CPU instead.  There }
  133. { are two types of applications for a timer - (1) EVENT TIMER: duration of a }
  134. { a single event, and (2) RATE TIMER: the number of events in a single       }
  135. { duration.  Our application in SYS is the latter.  Where resolution (not    }
  136. { accuracy) is low in the timer, it is gained in the CPU by making sure      }
  137. { there are a LARGE number of events.                                        }
  138. {                                                                            }
  139. { Notice that this timer is accurate to +/- 1 repetition as opposed to some  }
  140. { value in "seconds".  That's because it measures EVENTS, not seconds.  So,  }
  141. { a test of a given duration producing 100,000 events will certainly have    }
  142. { higher resolution than the same duration producing only 100 events.  The   }
  143. { former would have 0.001% resolution while the latter only 1%.  If you      }
  144. { increase the duration of the test, you can get even higher resolution.     }
  145. {                                                                            }
  146. { RATE is meant to be a test tool for SYS to give a rough idea of            }
  147. { comparative performance and works quite well.                              }
  148. { ========================================================================== }
  149.