Results 1 to 6 of 6

Thread: Measuring time intervals (accurately)- VB.net

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    7

    Measuring time intervals (accurately)- VB.net

    Hiya,

    I am writing visual stimuli software in VB.net for medical sciences. I need to accurately measeure time intervals, preferably to the nearest millisecond.

    From all I have heard, the Environment.TickCount property and the Timer control are not all that accurate.

    Im about to test each of them, but I was wondering if anyone had any other suggestions.

    Some code would be nice

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    This is what I use for basic benchmarking:

    Code:
    'Copyright (C) 2002 Microsoft Corporation
    'All rights reserved.
    'THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
    'EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
    'MERCHANTIBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
    
    'Requires the Trial or Release version of Visual Studio .NET Professional (or greater).
    
    Option Strict On
    
    Imports System.TimeSpan
    Imports System.DateTime
    
    Public Class TimeCheck
    
        Private m_dtmBegin As DateTime
        Private m_EndSpan As TimeSpan
    
        ' Begin the timecheck
        Public Sub [Begin]()
            m_dtmBegin = DateTime.Now
        End Sub
    
        ' End the timecheck
        Public Sub [End]()
            m_EndSpan = DateTime.Now.Subtract(m_dtmBegin)
        End Sub
    
        Public ReadOnly Property Milliseconds() As Long
            Get
                Milliseconds = CType(m_EndSpan.TotalMilliseconds, Long)
            End Get
        End Property
    
    End Class

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    7
    Yeah thanks for the code,
    I see it uses DateTime.Now. Old mate at http://www.mentalis.org/soft/class.qpx?id=8
    implies that DateTime.Now is not that accurate.

    For your interest I downloaded his stop watch code (which refrernces some high res counter in the kernel32 lib?) and compared it to your timecheck. The code I ran was as follows:

    Start your timecheck
    Start old mates stop watch

    Code or No Code

    Stop your timecheck
    Stop old mates stop watch

    Without code in the guts there was no variance on most occasions, however I started to pick up variances with more code (7 to 13 ms for 20 sec tests, -13 to 13 ms for 8 min tests).

    Im not sure where its coming from but I think I will go with the other stopwatch.

    Thanks again

  4. #4
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Why don't you post it here? I'd like to try it also.

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    7
    It was just a quick test but heres the code:

    ***************************************************
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Dim variables before the counters
    Dim result2 As Single
    Dim watch1 As New TimeCheck()
    Dim I As Integer
    Dim J As Integer = TextBox1.Text 'End for code loop
    Dim dummyvar As Double = 1

    'Declaring the stopwatch starts it.
    'Start and finish the stopwatch one line before the timecheck
    Dim Watch2 As New Org.Mentalis.Utilities.StopWatch()
    watch1.Begin()

    For I = 1 To J
    dummyvar = (dummyvar + 1) / dummyvar
    Next

    result2 = (Watch2.Peek() / CType(10, Single)).ToString()
    watch1.End()

    Debug.WriteLine("Using Org mentalis: " & result2 & " Using Slow learner: " & watch1.Milliseconds)
    Debug.WriteLine("Variance= " & result2 - watch1.Milliseconds)
    End Sub
    ************************************************


    This uses a form with a button (button1) and a textbox (textbox1). You will need to download the stopwatch class module from: http://www.mentalis.org/soft/class.qpx?id=8.

    When running it you will notice zero variance on most occasions in the abscence of code or when j=0. This suggests that the experiment is properly calibrated. With longer loops we start to see variances. This highlights that one is inaccurate, but it dosent spell out which one.
    ******************************
    But, my mum thinks I'm cool

  6. #6
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Thanks for posting it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width