tag:blogger.com,1999:blog-8259613.post8589518295527307746..comments2023-08-05T11:16:50.347+02:00Comments on Kristof Verbiest - Bite-size C#: Beware of the stopwatchKristofhttp://www.blogger.com/profile/01727380410232817527noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-8259613.post-83260693780610804652015-03-06T06:32:58.798+01:002015-03-06T06:32:58.798+01:00For future readers - this post is quite old, and w...For future readers - this post is quite old, and while the information <i>was</i> valuable, it's outdated now. Stopwatch is quite safe. See:<br /><br />https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408%28v=vs.85%29.aspxMatthttps://www.blogger.com/profile/13874962191525196146noreply@blogger.comtag:blogger.com,1999:blog-8259613.post-31447695182318454512013-08-10T04:12:26.995+02:002013-08-10T04:12:26.995+02:00This comment has been removed by the author.Matthttps://www.blogger.com/profile/13874962191525196146noreply@blogger.comtag:blogger.com,1999:blog-8259613.post-74998678805554949382012-01-14T21:48:15.500+01:002012-01-14T21:48:15.500+01:00Hi Kristof,
Why did you say that Environment.Tick...Hi Kristof,<br /><br />Why did you say that Environment.TickCount (that is in milliseconds) has a better precision than DateTime.UtcNow.Ticks (which is in hundreds of nanoseconds, or in 0.0001 milliseconds) ? DateTime seems to be 10000 times more accurate.<br /><br />In .Net 2.0 Environment.TickCount is based on WinAPI GetTickCount(), and DateTime.Ticks is based on GetSystemTimeAsFileTime().<br />These functions are system dependent, and even though resolution is different, results may have the same precision (if OS clock aren't able to increment only with relatively big intervals)<br /><br />And just for information: <br />The StopWatch (in 2.0) is based on QueryPerformanceCounter() if supported, or uses DateTime.UtcNow.Ticks if opposite.Ivan Pnoreply@blogger.comtag:blogger.com,1999:blog-8259613.post-62451730698092672202009-10-21T17:43:22.744+02:002009-10-21T17:43:22.744+02:00MSDN: "The Stopwatch measures elapsed time by...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. Use the Frequency and IsHighResolution fields to determine the precision and resolution of the Stopwatch timing implementation."<br />See:<br />http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspxAndriy Budayhttps://www.blogger.com/profile/09181254564747384052noreply@blogger.comtag:blogger.com,1999:blog-8259613.post-66102382469842316432009-06-29T08:56:17.880+02:002009-06-29T08:56:17.880+02:00@Anonymous: but how do you know when the frequency...@Anonymous: but how do you know when the frequency has changed? I don't think there is an event that you can subscribe to, and polling the frequency will not be very efficient.Kristofhttps://www.blogger.com/profile/01727380410232817527noreply@blogger.comtag:blogger.com,1999:blog-8259613.post-83414538045642427762009-06-27T17:27:46.887+02:002009-06-27T17:27:46.887+02:00Shouldn't you be able to use Stopwatch.Frequen...Shouldn't you be able to use Stopwatch.Frequency to normalize the ticks values in case the CPU frequency has changed?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8259613.post-33227986427125637882009-06-25T10:11:51.045+02:002009-06-25T10:11:51.045+02:00@ArabixTeam: based on the documentation I think th...@ArabixTeam: based on the documentation I think that Environment.TickCount has a resolution of 1 ms, which is better than DateTime.Now but worse than the StopWatch. So this might be a good alternative if the resolution of DateTime.UtcNow is not good enough for your application. I haven't done any testing regarding the performance implications, but I would guess that this just involves reading a counter. So the performance might be even better than using DateTime.UtcNow. I suggest you do your own test if performance is very critical for you. <br />Just be very careful that you handle the overflow situation well (every 49 days the the value will overflow from Int32.MaxValue to Int32.MinValue).<br />Thanks for bringing this to my attention. I never actually noticed that this property existed!Kristofhttps://www.blogger.com/profile/01727380410232817527noreply@blogger.comtag:blogger.com,1999:blog-8259613.post-13870748414499822322009-06-24T14:39:42.538+02:002009-06-24T14:39:42.538+02:00What about Enviroment.TickCount? you did not menti...What about Enviroment.TickCount? you did not mention any thing about it?ArabixTeam Memberhttps://www.blogger.com/profile/17448379706777666507noreply@blogger.comtag:blogger.com,1999:blog-8259613.post-17964630643607306382008-10-16T08:51:00.000+02:002008-10-16T08:51:00.000+02:00Thanks Kristof. I was asking more than arguing and...Thanks Kristof. I was asking more than arguing and this discussion is exactly the kind of thing I was hoping for. I thought your last tip was quite useful so I let my colleagues know about it and one responded that Stopwatch should be used for such timing, prompting me to make the comment.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8259613.post-79208768620609677212008-10-15T21:52:00.000+02:002008-10-15T21:52:00.000+02:00Very interesting. Great post.Very interesting. Great post.Anonymousnoreply@blogger.com