Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9020237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:58:02+00:00 2026-06-16T04:58:02+00:00

I need to implement simple performance benchmarking in Free Pascal. In Delphi I am

  • 0

I need to implement simple performance benchmarking in Free Pascal. In Delphi I am using TStopWatch record from Diagnostics unit, what can I use in Free Pascal/Lazarus?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:58:05+00:00Added an answer on June 16, 2026 at 4:58 am

    Here is an implementation modeled after Delphi online doc:

    {        High frequency stop watch implemntation.
             Copyright (c) 2012 by Inoussa OUEDRAOGO
    
             This source code is distributed under the Library GNU General Public License 
             with the following modification:
    
                - object files and libraries linked into an application may be
                  distributed without source code.
    
             This program is distributed in the hope that it will be useful,
             but WITHOUT ANY WARRANTY; without even the implied warranty of
             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
         **********************************************************************}
    
    {$IFDEF FPC}
      {$mode objfpc}{$H+}
      {$modeswitch advancedrecords}
    {$ENDIF}
    
    {$IFDEF MSWINDOWS}
        {$IFNDEF WINDOWS}
            {$DEFINE WINDOWS}
        {$ENDIF WINDOWS}
    {$ENDIF MSWINDOWS}
    
    unit stopwatch;
    
    interface
    uses
      SysUtils
      {$IFDEF LINUX}
      ,unixtype, linux
      {$ENDIF LINUX}
      ;
    
    type
    
      { TStopWatch }
    
      TStopWatch = record
      private
        const
          C_THOUSAND = 1000;
          C_MILLION  = C_THOUSAND * C_THOUSAND;
          C_BILLION  = C_THOUSAND * C_THOUSAND * C_THOUSAND;
          TicksPerNanoSecond   = 100;
          TicksPerMilliSecond  =  10000;
          TicksPerSecond       = C_BILLION div 100;
        Type
          TBaseMesure =
            {$IFDEF WINDOWS}
               Int64;
            {$ENDIF WINDOWS}
          {$IFDEF LINUX}
               TTimeSpec;
          {$ENDIF LINUX}
      strict private
        class var FFrequency : Int64;
        class var FIsHighResolution : Boolean;
      strict private
        FElapsed : Int64;
        FRunning : Boolean;
        FStartPosition : TBaseMesure;
      strict private
        procedure CheckInitialization();inline;
        function GetElapsedMilliseconds: Int64;
        function GetElapsedTicks: Int64;
      public
        class function Create() : TStopWatch;static;
        class function StartNew() : TStopWatch;static;
        class property Frequency : Int64 read FFrequency;
        class property IsHighResolution : Boolean read FIsHighResolution;
        procedure Reset();
        procedure Start();
        procedure Stop();
        property ElapsedMilliseconds : Int64 read GetElapsedMilliseconds;
        property ElapsedTicks : Int64 read GetElapsedTicks;
        property IsRunning : Boolean read FRunning;
      end;
    
    resourcestring
      sStopWatchNotInitialized = 'The StopWatch is not initialized.';
    
    implementation
    {$IFDEF WINDOWS}
    uses
      Windows;
    {$ENDIF WINDOWS}
    
    { TStopWatch }
    
    class function TStopWatch.Create(): TStopWatch;
    {$IFDEF LINUX}
    var
      r : TBaseMesure;
    {$ENDIF LINUX}
    begin
      if (FFrequency = 0) then begin
    {$IFDEF WINDOWS}
        FIsHighResolution := QueryPerformanceFrequency(FFrequency);
    {$ENDIF WINDOWS}
    {$IFDEF LINUX}
        FIsHighResolution := (clock_getres(CLOCK_MONOTONIC,@r) = 0);
        FIsHighResolution := FIsHighResolution and (r.tv_nsec <> 0);
        if (r.tv_nsec <> 0) then
          FFrequency := C_BILLION div r.tv_nsec;
    {$ENDIF LINUX}
      end;
      FillChar(Result,SizeOf(Result),0);
    end;
    
    class function TStopWatch.StartNew() : TStopWatch;
    begin
      Result := TStopWatch.Create();
      Result.Start();
    end;
    
    procedure TStopWatch.CheckInitialization();
    begin
      if (FFrequency = 0) then
        raise Exception.Create(sStopWatchNotInitialized);
    end;
    
    function TStopWatch.GetElapsedMilliseconds: Int64;
    begin
      {$IFDEF WINDOWS}
        Result := ElapsedTicks * TicksPerMilliSecond;
      {$ENDIF WINDOWS}
      {$IFDEF LINUX}
        Result := FElapsed div C_MILLION;
      {$ENDIF LINUX}
    end;
    
    function TStopWatch.GetElapsedTicks: Int64;
    begin
      CheckInitialization();
    {$IFDEF WINDOWS}
      Result := (FElapsed * TicksPerSecond) div FFrequency;
    {$ENDIF WINDOWS}
    {$IFDEF LINUX}
      Result := FElapsed div TicksPerNanoSecond;
    {$ENDIF LINUX}
    end;
    
    procedure TStopWatch.Reset();
    begin
      Stop();
      FElapsed := 0;
      FillChar(FStartPosition,SizeOf(FStartPosition),0);
    end;
    
    procedure TStopWatch.Start();
    begin
      if FRunning then
        exit;
      FRunning := True;
    {$IFDEF WINDOWS}
      QueryPerformanceCounter(FStartPosition);
    {$ENDIF WINDOWS}
    {$IFDEF LINUX}
      clock_gettime(CLOCK_MONOTONIC,@FStartPosition);
    {$ENDIF LINUX}
    end;
    
    procedure TStopWatch.Stop();
    var
      locEnd : TBaseMesure;
      s, n : Int64;
    begin
      if not FRunning then
        exit;
      FRunning := False;
    {$IFDEF WINDOWS}
      QueryPerformanceCounter(locEnd);
      FElapsed := FElapsed + (UInt64(locEnd) - UInt64(FStartPosition));
    {$ENDIF WINDOWS}
    {$IFDEF LINUX}
      clock_gettime(CLOCK_MONOTONIC,@locEnd);
      if (locEnd.tv_nsec < FStartPosition.tv_nsec) then begin
        s := locEnd.tv_sec - FStartPosition.tv_sec - 1;
        n := C_BILLION + locEnd.tv_nsec - FStartPosition.tv_nsec;
      end else begin
        s := locEnd.tv_sec - FStartPosition.tv_sec;
        n := locEnd.tv_nsec - FStartPosition.tv_nsec;
      end;
      FElapsed := FElapsed + (s * C_BILLION) + n;
    {$ENDIF LINUX}
    end;
    
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using CodeIgniter and need an easy to implement captcha. Something simple, nothing too
I need to implement a simple monitoring app in Excel. It is for monitoring
I need to implement a simple probe that uses open microsoft rdp protocol. But
I need to implement a simple logging mechanism for my web application. I have
I need to implement a simple and efficient XSS Filter in C++ for CppCMS
I need to implement a simp^le find function which will retrieve the 1st occurence
I need to implement portable code, but I do not know how to deal
I need to implement an efficient excel-like app. I'm looking for a data structure
I need to implement the following: There is a table A which is supposed
i need to implement the email signature with image.As of now we only support

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.