Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

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:

[crayon-673f2ed244216255000447/]

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:

[crayon-673f2ed244220487093042/]

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:

[crayon-673f2ed244222770830742/]

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:

[crayon-673f2ed244225760075448/]

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: 

[crayon-673f2ed244227280683320/]

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:


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.

Exit mobile version