Link to home
Start Free TrialLog in
Avatar of Dennis9
Dennis9

asked on

Calculate to times??

Hi all.
I use this to calculate how long the oparation took:

...
Var
  StartTime,
  EndTime: TTime;

...
//When process starts
StartTime := Time;

...
//When process end
EndTime := (Time + StrToTime(timelabel.caption)) - StartTime;
timelabel.caption := FormatDateTime('hh:nn:ss', EndTime);


It also work fine, but it don't calculate the seconds used. What i mean is that the process are not always very long ex. 1 sec. and then the timelabel is still 00:00:00
The process have to take more then 60 sec before something happens, on the timelabel.

I want to know, why it does not calculate the time correct?


Thanks.
Dennis
Avatar of FrodoBeggins
FrodoBeggins

Use the GetTickCount() function
var dProcess, dDif :Double;
begin
  dProcess:=GetTickCount();
  ...;//something
  dDif:=GetTickCount()-dProcess;
  ShowMessage('Finished in '+IntToStr(Trunc(dDif) div 60000)+' min, '+IntToStr((Trunc(dDif) mod 60000) div 1000)+' sec, '+IntToStr(Trunc(dDif) mod 1000)+' ms. ');
  dProcess:=GetTickCount();
  ...; // ready gor next thing
GetTickCount() is an API function which retrieves how miliseconds elapsed after the OS was started. The time is stored as a DWORD value. Therefore, the time will wrap around to zero if Windows is run continuously for 49.7 days.
ASKER CERTIFIED SOLUTION
Avatar of bugroger
bugroger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sometimes you may need sub-milliseconds...

var
  f,st,et: Int64;
begin
  QueryPerformanceFrequency(f);
  QueryPerformanceCounter(st);

  // Do something here

  QueryPerformanceCounter(et);
  ShowMessage(Format('took %f microseconds,[((et-st)*1000000)/f]));
end
after seting the caption value add line in your code:

timelabel.repaint;
for measuring small time period less then milliseconds you can use zlrdtsc library from http://www.specosoft.com .
It with sorces and free.
Avatar of Dennis9

ASKER

Sorry i had forgot this.
I accept your answer because there i both get the hours, min, sec and mil.

Thanks all