Accuracy of Timers in VB5 
Author Message
 Accuracy of Timers in VB5

I have an odd problem with timers in VB5.  I have two timers on a form.
One is set with an interval of 100 (one tenth of a second).  The other is
set to 1000 (one second).  Both timers are enabled by a command button
click.  During the timer event, both timers increment a long counter
variables.  Given the setup above, you would think that the value of the
first timer would be 10x the value of the second timer.  As it turns out,
the first timer is slow.  For example, the second timer will report a
value of 20 seconds (which is accurate with a stop watch), while the first
timer will report a value of 187 1/10th of a second.  The difference
between the two timers is not consistent.  If I set the interval on both
timers to 1000, the two counter values are still inconsistent.  Am I doing
something wrong here?  

Any insight into this problem would be helpful.

--rich

=====
Richard L. Anderson
Department of Behavior Analysis, University of North Texas
email: anderson at unt dot edu remove i.dont.want.your.spam in the reply-to



Sun, 09 Jul 2000 03:00:00 GMT  
 Accuracy of Timers in VB5


: timers to 1000, the two counter values are still inconsistent.  Am I doing
: something wrong here?  

: Any insight into this problem would be helpful.

no, but insight is the key!

you have to remembe what sort of system you are dealing with here--a
preemtive multitasking system (i direct you to Tannenbaum if you are
unfamiliar with these terms).  processes' share of the CPU can and will
change "randomly" (from the user's perspective).  While basic process
synchronization theory says that eventually every process will get to run,
very little (if anything) is guaranteed about when this will be so.  Add
in complications regarding threads and priorities of various sorts and the
underlying fact is magnified:  timing is not guaranteed in Windows 95 (NT
also, as far as I know..though a way back there was talk of NT for 'real
time' market data.. but of course, as far as i know 'real time' for market
data can mean seconds, not milliseconds).

Ok.. that understood, what can be done?  first, there is the issue that
the VB timer is not terribly fine grained.  despite that you can set the
interval to '1', the effective interval where it might actually work
in real life is closer to 12 or 55, depending on who you believe.  a
simpls solution is to replace this with a 'high-res' timer made by mabry
or fxtools... i have had success with these (for all practical purposes
the two work the same), being able to get perfomance down to about 2-3ms
(this also depends on the code you are running in the timer event).  would
2-3 ms be maintained if, say, i started loading MS-Word and/or running an
intense matlab simulation while this was going on? of course not.

beyond that, you can get rid of timers and go to api calls.  GetTickCount
is a good place to start your search.  again, NOTHING is guaranteed, but
you probably will be able to get a performance gain (at the expense of
having to restucture some of your code.. though some people have posted
here ways to emulate say, the VB timer with straight API calls, in temrs
of efficiency probably if you are going to go the 'all code'/'api' route,
your best bet is to restructure your code rather than trying to emaulate a
timer.

but you're saying "but adrian--i have this win95 pc hooked up to this
supercollider here and i need to sample the data at every 10 E -19
picoseconds so i will see whether the elusive 'Anderon Neutrino' really
exists".  well then, as far as Win95 and virtually every other O/S out
there (inclding virtually all 'out of the box' unix implementations), you
are SOL.  even if what you need is evey ms (say +- 5% of 1ms) and you are
in the PC world.. unless you know enough to get down to a hardware level
and look at clock interrupts and so on, good old DOS might be the way to
go.

- adrian



Sun, 09 Jul 2000 03:00:00 GMT  
 Accuracy of Timers in VB5

First off, the standard Timer control in VB is incapable of firing any
faster than once per 55 mS processor cycle, regardless of how low you set
the Interval property.  For instance, every 55 mS Windows looks to see if
the Timer's 'complete flag' is set true.  If it is, it executes the code
inside of the timer event.  If, however, the 'complete' flag is set 1 mS
*after* Windows checks the flag, it will be another 54 mS before Windows
gets around to checking again, so your counter lable will have lost as much
as 54 mS (worst case).

If you need to have 1 mS resolution, you can do this by either utilizing
CallBacks with AddressOf proceedures, or tight, system draining loops based
on the multimedia timeGetTime function.

--

Tony Selke

(please remove the _No-Junk-Mail_ prior to responding)

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

Quote:

>I have an odd problem with timers in VB5.  I have two timers on a form.
>One is set with an interval of 100 (one tenth of a second).  The other is
>set to 1000 (one second).  Both timers are enabled by a command button
>click.  During the timer event, both timers increment a long counter
>variables.  Given the setup above, you would think that the value of the
>first timer would be 10x the value of the second timer.  As it turns out,
>the first timer is slow.  For example, the second timer will report a
>value of 20 seconds (which is accurate with a stop watch), while the first
>timer will report a value of 187 1/10th of a second.  The difference
>between the two timers is not consistent.  If I set the interval on both
>timers to 1000, the two counter values are still inconsistent.  Am I doing
>something wrong here?

>Any insight into this problem would be helpful.

>--rich

>=====
>Richard L. Anderson
>Department of Behavior Analysis, University of North Texas
>email: anderson at unt dot edu remove i.dont.want.your.spam in the reply-to



Sun, 09 Jul 2000 03:00:00 GMT  
 Accuracy of Timers in VB5


Quote:

>I have an odd problem with timers in VB5.  I have two timers on a form.
>One is set with an interval of 100 (one tenth of a second).  The other is

Hello,

I have posted a solution some time ago. Please use Deja News to get my
posting to this group. Subject: Tired of the VB timer? Need a
precision timer?

The solution described there uses the Multimedia Timer of Windows 95
or Windows NT, the callback function of VB 5 is required, VB 4 can't
be used for that.

By
Uwe Hercksen

For EMail response, please replace nospam with zew
--------------------------------------------------
Elektronikwerkstatt Uni. Erlangen
Cauerstr. 5
D91058 Erlangen



Mon, 10 Jul 2000 03:00:00 GMT  
 Accuracy of Timers in VB5

Well, I like to do game programming in VB and one of the ways I have
used to gain speed on animation, where the timer would slow it down, was
to place in the Form Activate event the following:

Do
  Call My_Routine
  DoEvents
Loop

Of course, this had problems, so I learned recently the "correct" way to
do it. In a timer that is active and
has a tick value greater than 0, put:

Timer1.Enabled=False

Do
  Call My_Routine
  DoEvents
Loop

This works better - I have noticed speed increases of about 2x. This
isn't perfect - not millisecond accuracy.

What DOS game programmers do is redirect the timer (PIT) interrupt to
thier own routine, and reprogram the time to fire at a higher rate, then
within thier own routine, call the original timer routine at a rate that
is equal to the ration between it's old value and it new one. It is
pretty cumbersome, but it works out good.

I don't think you can get below 18 ticks per second, because windows
doesn't reprogram the PIT (if I am wrong, somebody please correct me).

My two cent's worth, anyhow...



Sat, 15 Jul 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Timer, accuracy in millisecs!

2. Timer Function Accuracy

3. vb timer accuracy ???

4. Timer Accuracy - Help !

5. Timer with high accuracy

6. Timer Accuracy

7. Wanted: VB3 Timer with millisecond accuracy.

8. Timer accuracy??? -RFH ( Request for Help)

9. timer accuracy of mmsystem.dll

10. Accuracy of timers in VB?

11. Timer accuracy

12. Timer accuracy

 

 
Powered by phpBB® Forum Software