|
-
May 15th, 2003, 12:36 AM
#1
Thread Starter
New Member
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
-
May 15th, 2003, 01:36 AM
#2
Fanatic Member
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
-
May 15th, 2003, 10:59 AM
#3
Thread Starter
New Member
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
-
May 15th, 2003, 02:42 PM
#4
Fanatic Member
Why don't you post it here? I'd like to try it also.
-
May 16th, 2003, 03:53 AM
#5
Thread Starter
New Member
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
-
May 16th, 2003, 07:47 AM
#6
Fanatic Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|