I
I
Ivan Balan2014-02-06 18:11:24
Delphi
Ivan Balan, 2014-02-06 18:11:24

How fast is a piece of code executed in Delphi?

Hello,
I need to find out how quickly this or that piece of code is executed in Delphi. For example, to measure the speed of execution of certain procedures or functions. I looked at examples on the Internet, but using GetTickCount it’s not very accurate, and sometimes it doesn’t give a result at all. Please help.
Tried to do like this:

spoiler
function GetCPUTick: Int64;
asm
   DB $0F,$31
end;

function CalibrateCPU: int64;
var
  t: cardinal;
begin
  t := GetTickCount;
  while t=GetTickCount do;
  Result := GetCPUTick;
  while GetTickCount<(t+400) do;
  Result := GetCPUTick - result;
  CPUClock := 2.5e-6*Result;
end;

function TicksToStr(const Value: int64): string;
begin
  Result := FloatToStrF(Value/CPUClock,fffixed,10,2)+ ' мс';
end;
_______________________________________________________________

CalibrateCPU;
  ticks := GetCPUTick;
    bubblesort(arr100,0,100);
  ticks := GetCPUTick - ticks;


But here are the results: 72000ms (10000 elements in the array). The result is obviously wrong, because there are a lot of

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2014-02-06
@Toddams

I used procedures which are already described in :
QueryPerformanceFrequency - an amount of cycles for millisecond
QueryPerformanceCounter - an amount of cycles
Your example:
var
tc,tc2,tcins:int64;
QueryPerformanceFrequency(tcins);
QueryPerformanceCounter(tc);
bubblesort(arr100,0,100);
QueryPerformanceCounter(tc2);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question