The Simple ZPL Timers

This page is meant to give a summary of how to use ZPL's machine dependent timing routines. If you find inconsistencies between this document and your use of ZPL, please let us know at zpl-bugs@cs.washington.edu.

Overview

ZPL provides two machine dependent timing routines: ResetTimer and CheckTimer.

	prototype double ResetTimer();
	prototype double CheckTimer();

Their functionality is similar to the older (now obsolete) routines, ZPLStartTimer and ZPLCheckTimer. Specifically, ResetTimer resets the ZPL timer. Though the timer is initialized at the beginning of every ZPL program, when it is reset is not defined and thus may vary between platforms. If absolute timings are required, it is advisable to call ResetTimer before CheckTimer is called. ResetTimer returns the elapsed time (in seconds) since the last call to ResetTimer (or since initialization). CheckTimer returns the elapsed time since the last call to ResetTimer. Notice that calling CheckTimer does not reset the timer.

The timing routines are mapped to fine-grained machine dependent timing routines where available. If no such routine exists gettimeofday is used.

Examples

The example below illustrate a few ways to use the timing routines. Always make sure that the granularity of the clock is fine enough to time the piece of code you are interested in. If the values returned by the timing routines are less than the granularity of the timer on a platform, the times are probably inaccurate.

The following program prints out the number of seconds it takes to run the main loop of the program.

   program timertest1;
   procedure timertest1();
   var runtime: double;
       i: integer;
   begin
     -- initialize the timer
     ResetTimer();
     for i := 0 to 1000 do
       write(".");
     end;
     runtime := CheckTimer();
     writeln();
     writeln(runtime, " seconds");
   end;

The following program prints out the number of seconds it take to run each iteration of main loop of the program.

   program timertest2;
   procedure timertest2();
   var runtime: double;
       i: integer;
   begin
     -- initialize the timer
     ResetTimer();
     for i := 0 to 1000 do
       write("iteration ", i);
       runtime := ResetTimer();
       writeln(runtime, " seconds.");
     end;
   end;

The following program prints out the number of seconds it take to run the program, checkpointing at each iteration.

   program timertest3;
   procedure timertest3();
   var runtime: double;
       i: integer;
   begin
     -- initialize the timer
     ResetTimer();
     for i := 0 to 1000 do
       write("iteration ", i);
       runtime := CheckTimer();
       writeln(runtime, " seconds so far.");
     end;
   end;

Further Questions?

If you have further questions about timers in ZPL, please don't hesitate to contact us at zpl-info@cs.washington.edu.