Link to home
Start Free TrialLog in
Avatar of comicboy
comicboy

asked on

About Timer

I'm doing some simulation program and using TTimer with D7,
Something weird happen :
1. On system P4 2,4 with HT which the first time I'm build the program its running fine
2. On system AMD64 3200+ which I'm continue the program the speed of the simulation became very slow which force me to decrease the interval value of TTimer and the simulation run weird the speed is not constant, sometime very fast and sometime normal
3. Back on another P4 2,4 non HT it became slowest even I've alread reduce TTimer from 250 interval to just 50

Is there anyway I can use constant speed Timer for all machine ?
Please make a procedure or function which is easy to use for my Timer.

Thank You
Avatar of kretzschmar
kretzschmar
Flag of Germany image

you may use another timer-component,
the TTimer-component is not very accurate in its intervall
Avatar of alikoank
alikoank

I get from your question that your problem is not with the timer, its about how fast a computer can run your simulation program.

You can run a short part of your simulation on program startup and measure how much time does it take to run. Then you can adjust your timer interval based on that value. As there are indefinite number of computers out there in an indefinite number of configurations, you should not expect an exact value. So:

>> Is there anyway I can use constant speed Timer for all machine ?

no, there is not.

You can give user the option to adjust timer interval as he/she wishes.
To measure the times of diffrerent functions I use the following code, It can time down to a millionth of a second or so! If you can find out which procedures are taking too long then you can try to speed them up.  If you set your programs timer quite slow for everyone then there should be less of a difference when going between computers

The code is

function getStartSecs:extended;
var
  freq,start:int64;
begin
  QueryPerformanceFrequency(freq); {Get counts/second}
  queryperformanceCounter(start);
  result:=start/freq;
end;

function getElapsedSecs(const start:extended):extended;
var
  freq, count:int64;
begin
  QueryPerformanceFrequency(freq); {Get counts/second}
  queryperformanceCounter(count);
  result:=(count/freq-start);
end;

procedure TForm1.Button2Click(Sender: TObject);
 var
  start,stop1,stop2,freq:int64;
  i,j:integer;
  Times: integer;
  TotalTicks,NumASec: int64;
  Ticksfor1,Timefor1: real;
  x:extended;
begin
  Times:= {ENTER NUMBER OF TIME TO RUN THE PROGRAM 100+ RECOMENDED}

  screen.cursor:=crHourGlass;
  QueryPerformanceFrequency(freq); {Get counts/second}
  QueryPerformanceCounter(start); {Get string count}
  x:=0;
  for i:=1 to Times do ;
  QueryPerformanceCounter(stop1);  {Get end of loop1 count}

  for i:=1 to Times do  {TEST PROCEDURE GOES IN HERE}

   QueryPerformanceCounter(stop2); {Get end of loop2 count}
  screen.cursor:=crDefault;
  {calculate net counts per million processmessage calls and
   if we divide by freq (counts/sec), we get secs per million calls
  (i.e. microsecs per call)
 }

  TotalTicks:= (stop2-stop1)-(stop1-start);
  Ticksfor1:= TotalTicks/Times;
  Timefor1:= Ticksfor1 / freq;
  NumASec:= Trunc(1{second} / Timefor1);

  If freq>0 then
     showmessage('x='+floattostr(x)
     +#13+'Ticks taken to call the procedure '+inttostr(Times)+' times:= '+
     inttostr(TotalTicks)+
     #13+'      Ticks for 1 Call:= '+FloatToStrF(Ticksfor1,ffNumber, 4,2)+
     #13+'      Ticks per second:= '+inttostr(freq)+
     #13+'      Time in micro seconds for 1 call:= '+FloatToStrF(1e6*Timefor1,ffNumber, 4,8)+
     #13+'      Therefore you can call this procedure '+floattostr(NumASec)+' times a second')
  else showmessage('No hardware timer available');

end;

You will need to add Msystem and Maths to your uses.  The source has been modified from www.delphiforfun.org

hope that helps

DAvid
ASKER CERTIFIED SOLUTION
Avatar of Wim ten Brink
Wim ten Brink
Flag of Netherlands image

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
timer interval is totally dependant on the system clocking, so interval 1000 miliseconds is 1000 physical milliseconds, no matter what computer you running it on, 286 or P4 or AMD64. You may have a problem with running procedure inside timer, on P4 i may takes less time to process and AMD64 a little longer, then timer starts slipping. Look at the procedure/function execution