I want to find the execution time of a program I wrote in pascal but i dont know the syntax to do that.. is there a function that calculates it? I searched a little bit and found that people use the type TDateTime and give to a var the value now and to another var the value now (one before the program that we want to have its execution time and one after) and then calculate the difference between the two values. But I cant really do that because I would need the DateUtils lib (which i dont have??).. Is there another way to calculate the execution time??
(I'm using windows 10)
Use the unit SysUtils and from that the Now() function - it will give you the current date and time. Call it once before and once after whatever you want to measure. Since TDateTime is in fact only a Double you can operate arithmetically on it (subtraction) - that way you can later just pull each time (and date) component to see how long it lasted:
program TimeDiff;
uses
SysUtils; // Every Pascal implementation should have this
// Just pointless calculations to waste CPU cycles
function RoutineToMeasure(): Integer;
var
i, j: Integer;
begin
result:= 0;
for i:= 0 to $7FFFFFFF do begin
j:= i div 24;
j:= j+ 10;
result:= result xor j;
result:= result or i;
end;
end;
// The actual program to measure times
var
tStart, tEnd, tDiff: TDateTime; // Begin and end of measurement, and difference
iHours, iMinutes, iSeconds, iMilliseconds: Word; // Time components
begin
tStart:= Now(); // Get date+time
RoutineToMeasure();
tEnd:= Now(); // Get date+time again
// To be read by a human
Writeln( ' started: ', DateTimeToStr( tStart ) );
Writeln( ' finished: ', DateTimeToStr( tEnd ) );
// Calculating the actual time it took
tDiff:= tEnd- tStart; // Subtract higher from lower value
DecodeTime( tDiff, iHours, iMinutes, iSeconds, iMilliseconds ); // Does not account for day changes when executing around midnight
Writeln( ' taken: ', iHours, ' h, ', iMinutes, ' min, ', iSeconds, ' sec, ', iMilliseconds, ' msec' );
Readln();
end.
Successfully tested in Lazarus 2.2.0 with FCP 3.2.2; the output is as per a 4 core 3.2 GHz CPU:
started: 2022-12-14 14:33:07 finished: 2022-12-14 14:33:12 taken: 0 h, 0 min, 5 sec, 323 msec
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With