Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
CodeDelphiTech Partner

How To Record Exact Operation Execution Time With Delphi

How To Record Exact Operation Execution Time With Delphi
How To Record Exact Operation Execution Time With Delphi Softacom header

To know how to get the information on the exact execution time of an operation in your Windows and cross platform apps can be useful in all sorts of circumstances. For example, it can be necessary when you need to show users the time spent on the execution of a long operation (by the way, in such a situation, high accuracy is rarely required). Or you may need to know the exact time needed for executing a particular operation while you optimize the software. A high accuracy and dependable timer can be a key part of this by identifying the most challenging places in the program where it “gets stuck” for a long period of time. Usually, the latter situation is typical when you are working on programs that use and process huge volumes of data, and the speed of operations may be not the highest priority but still one of the most crucial requirements for the application.

Understanding the quickest and slowest parts of your executable code is part of the concept of software metrics – measuring things. Getting a full understanding of this and other metrics such as maximum memory usage, network traffic throughput, data operations, and so on, allows you to plan future development and identify areas where there is room for improvement.

There are several ways to check the execution time of operations in Delphi.  In this article, we examine several possible ways to do it.

How do I use the Now() function to time areas of my app?

The simplest but the least accurate way to measure the time that is necessary to execute an operation in Delphi is to use the Now() function in the System.SysUtils module.

The source code may look as follows:

It is highly likely that you may have the following question: Why did we use SecondsBetween() in the example, and not, for instance, MilliSecondsBetween() that can ensure greater accuracy? We rely on the description of the Now() function in the official Delphi Help. It reads: “Although TDateTime values can represent milliseconds, Now is accurate only to the nearest second.” It means that if you use Now(), there is no sense in determining the time interval with an accuracy of milliseconds.

How can I use the Windows API and the GetTickCount() function to time areas of my app?

The GetTickCount() function has no parameters and retrieves the number of milliseconds that have elapsed since the system was started. According to Microsoft’s official documentation, the resolution of the GetTickCount() function is limited to the resolution of the system timer, which is typically in the range of 10 to 16 milliseconds. The millisecond counter will be reset if the system has been running for more than 49.7 days.

In general, the example of using this function is similar to the previous one:

So, with the GetTickCount() function, we can track the execution time of an operation in Delphi with millisecond accuracy. If such accuracy does not suit you and you need to measure the time interval even more precisely, then the next method under consideration is for you.

How do I use the QueryPerformanceCounter and QueryPerformanceFrequency functions to time functions in my app?

QueryPerformanceCounter – Retrieves the current value of the performance counter, which is a high resolution (<1us) timestamp that can be used for time-interval measurements.

QueryPerformanceFrequency – Retrieves the frequency of the performance counter. The frequency of the performance counter is fixed at system boot and is consistent across all processors. That’s why you need to request the frequency only upon application initialization, and the result can be cached.

In order to use these functions to count the time interval needed for the execution of any operation in Delphi, we need to arrange the source code, for example, the following way:

You can work with the obtained values ​​of T1 and T2 as you wish, for example, you can display minutes / seconds / milliseconds separately, etc. This depends on your needs and desires. In this article, I showed the simplest example of using a high-resolution counter in Delphi.

How to use the capacities of Delphi. System.Diagnostics module to time functions in my app?

The System.Diagnostics module was introduced in Delphi rather long ago. The module provides only one record – TStopwatch. It is essentially a convenient “wrapper” for using high-resolution timers in your cross-platform apps. TStopwatch uses the functionality that depends on the operating system to access high-resolution timers if they are available. If high-resolution timers are not available in the OS, usual timers are used.

Even though TStopwatch is a record, you still need to call the Create or StartNew method to use it correctly.

The description of TStopwatch is the following:

  • The IsHighResolution property specifies whether the timer is based on a high resolution performance counter.
  • The Start() method starts measuring elapsed time.
  • The Stop() method stops measuring elapsed time.
  • The ElapsedMilliseconds property gets the total elapsed time in milliseconds.
  • The Elapsed property gets the elapsed time in the form of TTimeSpan.

Is there an example of using a TStopWatch to time functions in my app?

It is quite simple to use the capabilities of TStopwatch. For example, you can do it the following  way: 

In the example above, we used TStopwatch and displayed the number of minutes (with a fractional part) that were necessary for performing a particular operation. In general, using TStopwatch.Elapsed, you can display any values, limited only by the TTimeSpan features.

What is the best way to record the execution time of functions in my application?

To measure the exact execution time of an operation in Delphi, first of all, you need to decide what accuracy suits you:

  • If second accuracy is enough, you can use the usual, well-known Now () function. Yes, the accuracy is the lowest, but, on the other hand, this method is absolutely simple.
  • If you want to reach millisecond accuracy, use GetTickCount() it is simple and reliable, but only if you do not plan to measure a time interval that will be longer than 49.7 days.
  • If you need to use the most accurate methods for measuring the execution time – in a Windows application, then we recommend using either a combination of the QueryPerformanceCounter() and QueryPerformanceFrequency() functions.
  • Finally, the most convenient method is the use of TStopwatch in the System.Diagnostics module. This is the most suitable for cross-platform apps since it works on anything including Windows, Linux, iOS, Android, and macOS.
How To Record Exact Operation Execution Time With Delphi Delphi logo

Do you want to try some of these examples for yourself? Why not download a free trial of the latest version of RAD Studio with Delphi?

This article was written by Embarcadero Tech Partner Softacom. Softacom specialize in all sorts of software development focused on Delphi. Read more about their services on the Softacom website.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.