I would like to know how to calculate the time consumed for a function in Delphi.
Then I wanted to show the used time and compare it with another function or component so as to know the faster function.
I would like to know how to calculate the time consumed for a function in Delphi.
Then I wanted to show the used time and compare it with another function or component so as to know the faster function.
For the sake of having more possibilities for tackling the question, you could also use
System.Classes.TThread.GetTickCount
to get a current time in milliseconds to start your timer before your method, and then again after your method. The difference between these two is obviously the elapsed time in milliseconds, which you could transform into hours, seconds, etc.Having said that, David Heffernan's proposal with
TStopwatch
is more elegant (and more precise?).You can use
TStopwatch
from theSystem.Diagnostics
unit to measure elapsed time using the system's high-resolution performance counter.To read a time value in seconds, say, from a time span, do this:
You can use the
QueryPerformanceCounter
andQueryPerformanceFrequency
functions: