Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,709 questions

55,473 answers

573 users

How to measure function execution time in Pascal

3 Answers

0 votes
// Measuring execution time using GetTickCount

program MeasureTime;

uses
  SysUtils;  // On Linux: SysUtils, On Windows: Windows;

procedure Work;
var
  i: LongInt;
  sum: Int64;
begin
  sum := 0;
  for i := 1 to 100000000 do
    sum := sum + i;
end;

var
  startMS, endMS: QWord;
  elapsedMS: QWord;
  seconds: Double;

begin
  // Record start time in milliseconds
  startMS := GetTickCount64;

  Work;

  // Record end time
  endMS := GetTickCount64;

  elapsedMS := endMS - startMS;
  seconds := elapsedMS / 1000.0;

  WriteLn('Execution time: ', elapsedMS, ' ms');
  WriteLn('Execution time: ', seconds:0:6, ' seconds');
end.



(* 
run:

Execution time: 198 ms
Execution time: 0.198000 seconds

*)

 



answered May 27 by avibootz
0 votes
// Measuring execution time using Now and Milliseconds

program MeasureTime;

uses
  SysUtils, DateUtils;

procedure Work;
var
  i: LongInt;
  sum: Int64;
begin
  sum := 0;
  for i := 1 to 100000000 do
    sum := sum + i;
end;

var
  startTime, endTime: TDateTime;
  elapsedMS: Int64;
  seconds: Double;

begin
  // Record start time
  startTime := Now;

  Work;

  // Record end time
  endTime := Now;

  elapsedMS := MilliSecondsBetween(endTime, startTime);
  seconds := elapsedMS / 1000.0;

  WriteLn('Execution time: ', elapsedMS, ' ms');
  WriteLn('Execution time: ', seconds:0:6, ' seconds');
end.



(* 
run:

Execution time: 180 ms
Execution time: 0.180000 seconds

*)

 



answered May 27 by avibootz
0 votes
// Measuring function execution time in Free Pascal using a reusable Timer (milliseconds + seconds)

program MeasureTimeWithReusableTimer;

{$mode objfpc}{$H+}

uses
  SysUtils;

// ---------------------------
// Reusable Timer definition
// ---------------------------
type
  TTimer = record
    StartMS: QWord;
    EndMS: QWord;
  end;

// Start the timer
procedure TimerStart(var T: TTimer);
begin
  T.StartMS := GetTickCount64;
end;

// Stop the timer
procedure TimerStop(var T: TTimer);
begin
  T.EndMS := GetTickCount64;
end;

// Return elapsed time in milliseconds
function TimerElapsedMilliseconds(const T: TTimer): QWord;
begin
  Result := T.EndMS - T.StartMS;
end;

// Return elapsed time in seconds
function TimerElapsedSeconds(const T: TTimer): Double;
begin
  Result := (T.EndMS - T.StartMS) / 1000.0;
end;

// ---------------------------
// Function to measure
// ---------------------------
procedure Work;
var
  i: LongInt;
  sum: Int64;
begin
  sum := 0;
  for i := 1 to 100000000 do
    sum := sum + i;
end;

// ---------------------------
// Main program
// ---------------------------
var
  T: TTimer;
  ms: QWord;
  sec: Double;

begin
  TimerStart(T);
  Work;
  TimerStop(T);

  ms := TimerElapsedMilliseconds(T);
  sec := TimerElapsedSeconds(T);

  WriteLn('Execution time: ', ms, ' ms');
  WriteLn('Execution time: ', sec:0:6, ' seconds');
end.



(* 
run:

Execution time: 196 ms
Execution time: 0.196000 seconds

*)

 



answered May 27 by avibootz
...