|
-
Mar 1st, 2007, 05:17 AM
#1
Thread Starter
Frenzied Member
[2005] Timer accuracy differences?
My friend has asked me to code a small program. It needs to simply display a letter after a certain amount of time and then asks the user to press the Spacebar as quickly as possible once the item appears on the screen.
That's not the issue, I can code that fine. I'm really just wondering about the accuracy of the timer. I mean, should I use TimeSpan, GetTimeStamp to work out the difference in time between onset and pressing of the key or the stopwatch (Elapsed time) or something else entirely??
From MSDN: "The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise, the Stopwatch class uses the system timer to measure elapsed time."
Which would suggest Stopwatch would be better if run on a reasonably good computer because if possible it will use the better of the two options when available? It also runs under System.Diagnostics which is different from the timer class suggesting a more hardware based approach (system ticks)...?
I guess my question is which timing method would be best?? Think I've just got myself a little confused.
Last edited by stimbo; Mar 1st, 2007 at 05:33 AM.
-
Mar 1st, 2007, 05:27 AM
#2
Re: [2005] Timer accuracy differences?
Does the StopWatch class uses QueryPerformanceCounter?
"The dark side clouds everything. Impossible to see the future is."
-
Mar 1st, 2007, 05:30 AM
#3
Thread Starter
Frenzied Member
Re: [2005] Timer accuracy differences?
From MSDN:
The Stopwatch class assists the manipulation of timing-related performance counters within managed code. Specifically, the Frequency field and GetTimestamp method can be used in place of the unmanaged Win32 APIs QueryPerformanceFrequency and QueryPerformanceCounter.
Would this be the way to go then? Using GetTimeStamp?
-
Mar 1st, 2007, 06:11 AM
#4
Re: [2005] Timer accuracy differences?
The most precise method would be to use the CPU ticks count since. I often use a great sample from exampleNet to demonstrate c++ accuracy. It can be found with 'CPU RDTSC' search string for VB 2005 (there is code for 2003 too).
---------
EDIT: I just noticed you prefer a managed solution
-
Mar 1st, 2007, 06:43 AM
#5
Thread Starter
Frenzied Member
Re: [2005] Timer accuracy differences?
Thanks for the link, there are a few things there that are interesting for me!
Although that code may be a little full on. My friend has a very limited knowledge of VB and I'm trying to write something that he can adapt himself - so I don't keep getting bothered
I think the stopwatch.ElapsedMilliseconds should be fine when run with a high resolution counter.
I'll mark as resolved but if anyone has another opinion please feel free to post or PM me.
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
|