Solved

About Timer

Posted on 2004-08-04
5
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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