TimerLoop.mesa
Copyright © 1992 by Xerox Corporation. All rights reserved.
Bier, December 8, 1992 10:48 pm PST
Contents: Provides an accurate estimate of the time taken by a given Cedar code fragment by running that fragment many times and subtracting away the loop overhead.
DIRECTORY
IO, Rope;
TimerLoop: CEDAR DEFINITIONS = BEGIN
ROPE: TYPE = Rope.ROPE;
STREAM: TYPE = IO.STREAM;
TimeForAPeriod: PROC [init: PROC, doit: PROC, periodMillisecs: CARD ¬ 2000] RETURNS [avgMicrosecs, initAvgMicrosecs: REAL ¬ 0.0, count, trials: CARD, worstAvg, worstInitAvg: REAL ¬ 0.0];
doit is a routine containing the code fragement you wish to time. init is a routine that will be called before each call to doit. The time to perform init and the procedure overhead of calling doit will both be subtracted out. periodMillisecs is a minimum period of time to use to run the test. The higher this number, the more accurate the results are likely to be. 2000 is a good number for short fragments. Based on periodMillisecs, TimeForAPeriod calculates how many trials to perform and how many times to run the fragment per trial. The fragment is run a total of count*trials times. avgMicrosecs is the average time for the fastest trial observed.
ReportForAPeriod: PROC [f: STREAM, name: ROPE, init: PROC, doit: PROC, periodMillisecs: CARD];
Calls TimeForAPeriod and prints out the results on stream f in a human-readable format.
TimeByCount: PROC [init: PROC, doit: PROC, count: CARD] RETURNS [totalMicrosecs, initTotalMicrosecs: CARD, avgMicrosecs, initAvgMicrosecs: REAL];
TimeForAPeriod is based on TimeByCount, which you may also wish to call directly. This routine returns the results of calling init and doit count times. Note: It may call init and doit more than count times if it discovers that machine performance varied greatly over the trial.
END.