Hi,
I don't know which precision you need for time, but the following unit does all right for me; it can be modified for going down to millisecond resolution.
Silvio a Beccara
------------------------------------------------------------------------------------ unit times;
interface function times ( tend: boolean; var tinit: double ): double;
implementation uses SysUtils, types;
function times ( tend: boolean; var tinit: double ): double;
var seconds: double; TS : TTimeStamp; DT : TDateTime;
begin
if tend = false then begin TS:=DateTimeToTimeStamp(Now); tinit := TimeStampToMSecs ( TS ); end
else begin TS:=DateTimeToTimeStamp(Now); seconds := ( TimeStampToMSecs ( TS ) - tinit ) / 1000; result := seconds; end; end; end.
------------------------------------------------------------------------------------
| Hi, all | | There have been some recent improvements to the Mersenne Twister (the | random number generator that is used in the GPC unit), and I've been | translating them into Pascal for eventual submission to the programming | team. As part of this, I wanted to check the execution time, so I wrote a | timer (I thought!) into a test program: | | program testMT; | uses GPC, MTrand; {MTrand is the name of the unit I'm working on} | const | big = 10000000; | TimeFormat = ' %Q'; | var | t1, t2, i, j : medCard; | CurrentTime : TimeStamp; | | begin | SeedRand(1234567890); | GetTimeStamp (CurrentTime); | WriteStr(FormatTime(CurrentTime, TimeFormat), t1); { line 17 } | for i := 1 to big do j := randInt; | GetTimeStamp (CurrentTime); | WriteStr(FormatTime(CurrentTime, TimeFormat), t2); | Writeln('Elapsed time: ', (t2 - t1):1); | end. | | The compiler hangs with the following error: | testmt.pas(17): Error: reference expected, value given | testmt.pas(17): Error: reference expected, value given | testmt.pas(17): Error: tree check: expected class 't', have 'x' | (error_mark) in require_complete_type, at p/typecheck.c:63 | Please submit a full bug report | | I'm guessing that it's not about my unit, but rather what I'm trying to do | with capturing a string into an int... anyway, here is a bug report. | | Toby
Hi, all
I wrote: | begin | SeedRand(1234567890); | GetTimeStamp (CurrentTime); | WriteStr(FormatTime(CurrentTime, TimeFormat), t1); { line 17 } [..] | The compiler hangs with the following error: | testmt.pas(17): Error: reference expected, value given | testmt.pas(17): Error: tree check: expected class 't', have 'x' | (error_mark) in require_complete_type, at p/typecheck.c:63 | Please submit a full bug report
I work with strings so seldom that I overlooked the (duh!) obvious: line 17 should be ReadStr(Format......, not WriteStr(...
Silvio's timer is way better anyway.
The bug report stands.
Toby
Toby Ewing wrote:
Silvio's timer is way better anyway.
Both versions (and Russ's as well) measure real time. For a benchmark it's better to measure CPU time. This is most easily accomplished from a shell:
time ./myprog
will run ./myprog and print the CPU time it consumed, without any special provision necessary in the program.
Within a program you can use GetCPUTime (unit GPC).
The bug report stands.
Indeed. The "reference expected" error is correct, but the tree check is a bug. I'll fix it when I get to it ...
Frank