3

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)

4
  • If you want to measure the time the entire program takes, you’ll need to use an external program such as time. Did you mean you want to measure the time a subroutine takes? Commented Dec 13, 2022 at 16:21
  • yes I meant the execution time of a subroutine not the entire program.. Commented Dec 13, 2022 at 16:39
  • Which OS are you using? Commented Dec 13, 2022 at 16:44
  • 2
    Please locate the Edit button beneath the tags of your question and edit your question to make it clear, that you want to measure the execution time of a subroutine. Also include the requested OS information. Commented Dec 13, 2022 at 16:47

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Do you know of something more precise than 1 ms?
Of course: FILETIME is up to 0.1 µs (f.e. used with GetSystemTimeAsFileTime()). See also QueryPerformanceCounter and others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.