none
Timer question: accuracy of timer? RRS feed

  • Question

  • Gurus: Any ideas please...

    I am wanting to print out the time elapsed between events accurately (a reaction time test actually).  The code will be to start the timer on presenting a visual image, stopping on a key press.  To simplify things for my simple brain I wrote a simplified program leaving out the presentation of the visual image and the keypress.

    I wrote the following simple code:

    Timer.Interval = 1000
    Timer.Tick = OnTick
    startTime = Clock.ElapsedMilliseconds
    Sub OnTick
      nowTime= Clock.ElapsedMilliseconds
      elapsedTime = nowTime-startTime
      TextWindow.WriteLine(elapsedTime/1000)
    EndSub

    How come it lists:

    1.00066
    2.00002
    3.00129
    4.00159
    5.00211
    6.00245
    7.00328

    NOT 

    1.00000
    2.00000
    3.00000
    4.00000
    5.00000

    ??

    Is there a way to correct for this?

    Any ideas?

    Many thanks, Skyerosebud

    Tuesday, April 2, 2019 9:28 AM

Answers

  • A couple milliseconds is quite normal for timer and ellapsed milliseconds.  Events are queued and don't neccessarily happen instantly, each line takes a certain amount of time etc.

    There are ways (beyond SB) to do quite accurate timing, but you can never expect an event to happen instantly.

    LDStopwatch is more accrate than Clock, usually used to performance test code snippets.

    Timer.Interval = 1000
    sw = LDStopwatch.Add()
    LDStopwatch.Start(sw)
    startTime = LDStopwatch.ElapsedMilliseconds(sw)
    Timer.Tick = OnTick
    Sub OnTick
      elapsedTime = (LDStopwatch.ElapsedMilliseconds(sw)-startTime)/1000
      TextWindow.WriteLine(elapsedTime)
    EndSub

    However, the Timer events are not guaranteed to happen exactly 1 second apart and other timings might be affected by anything else your PC is doing.

    Human reaction times should be over 100ms for anything so I think you should be OK.

    Tuesday, April 2, 2019 5:56 PM
    Moderator

All replies

  • A couple milliseconds is quite normal for timer and ellapsed milliseconds.  Events are queued and don't neccessarily happen instantly, each line takes a certain amount of time etc.

    There are ways (beyond SB) to do quite accurate timing, but you can never expect an event to happen instantly.

    LDStopwatch is more accrate than Clock, usually used to performance test code snippets.

    Timer.Interval = 1000
    sw = LDStopwatch.Add()
    LDStopwatch.Start(sw)
    startTime = LDStopwatch.ElapsedMilliseconds(sw)
    Timer.Tick = OnTick
    Sub OnTick
      elapsedTime = (LDStopwatch.ElapsedMilliseconds(sw)-startTime)/1000
      TextWindow.WriteLine(elapsedTime)
    EndSub

    However, the Timer events are not guaranteed to happen exactly 1 second apart and other timings might be affected by anything else your PC is doing.

    Human reaction times should be over 100ms for anything so I think you should be OK.

    Tuesday, April 2, 2019 5:56 PM
    Moderator
  • Ok great thanx
    Thursday, April 4, 2019 11:34 PM