Solved

About Timer

Posted on 2004-08-04
5
290 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:comicboy
5 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 11712885
you may use another timer-component,
the TTimer-component is not very accurate in its intervall
0
 
LVL 4

Expert Comment

by:alikoank
ID: 11712930
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.
0
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 11713257
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
0
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 125 total points
ID: 11714688
The speed of the TTimer interval also depends on the number of messages your application must handle. The ttimer just sends a message to your mainform and the mainform then has to handle it once it gets processing time. In a busy environment, the messageloop might just be busy handling a lot of things...
On a hyperthreading system you have the advantage that the system can handle multiple threads a lot better and thus your messageloop can run on one pseudoprocessor while other things run on the other pseudoprocessor. Systems without HT are having a bit more trouble since they have to do everything by themselves.

For a constant speed, consider using a separate thread that sleeps for the amount of time in-between actions and then sends a message to the main thread that an action needs to be executed without waiting for the message to be processed. That way, the thread will be pretty constant in it's timing. But your main thread might be a bit behind in the processing of these messages...
0
 
LVL 7

Expert Comment

by:FDzjuba
ID: 11714928
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
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now