stopwatch

How accurate is Thread.Sleep(TimeSpan)?

蹲街弑〆低调 提交于 2019-11-26 16:40:42
I've come across a unit test that is failing intermittently because the time elapsed isn't what I expect it to be. An example of what this test looks like is: Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); TimeSpan oneSecond = new TimeSpan(0, 0, 1); for(int i=0; i<3; i++) { Thread.Sleep(oneSecond); } stopwatch.Stop(); Assert.GreaterOrEqual(stopwatch.ElapsedMilliseconds, 2999); Most of the time this passes but it has failed on at least on one occasion failed because: Expected: greater than or equal to 2999 But was: 2998 I don't understand how it could possibly be less than 3 seconds.

Raise event in high resolution interval/timer

◇◆丶佛笑我妖孽 提交于 2019-11-26 14:42:14
I want to use the highest possible resolution timer using c#. For example, I want to raise an event every 11 ticks (I've heard that tick is the highest possible counter in pc). I tried timer and found that minimum elapsed time is in milliseconds. I looked at stopwatch but stopwatch doesn't raise events. Thanks. CodingBarfield Using a multimedia timer should give you about 1000 events per second. This code should help you on the way. public delegate void TimerEventHandler(UInt32 id, UInt32 msg, ref UInt32 userCtx, UInt32 rsv1, UInt32 rsv2); /// <summary> /// A multi media timer with millisecond

Stopwatch vs. using System.DateTime.Now for timing events [duplicate]

你。 提交于 2019-11-26 11:49:58
This question already has an answer here: Is DateTime.Now the best way to measure a function's performance? 15 answers I wanted to track the performance of my code so I stored the start and end time using System.DateTime.Now . I took the difference between the two as the time my code to execute. I noticed though that the difference didn't appear to be accurate. So I tried using a Stopwatch object. This turned out to be much, much more accurate. Can anyone tell me why Stopwatch would be more accurate than calculating the difference between a start and end time using System.DateTime.Now ? BTW, I

How accurate is System.Diagnostics.Stopwatch?

纵饮孤独 提交于 2019-11-26 08:28:39
问题 How accurate is System.Diagnostics.Stopwatch ? I am trying to do some metrics for different code paths and I need it to be exact. Should I be using stopwatch or is there another solution that is more accurate. I have been told that sometimes stopwatch gives incorrect information. 回答1: Why you don't profile your code instead of focusing on micro-benchmarks? There are some good Open Source profilers like: NProf Prof-It for C# NProfiler ProfileSharp 回答2: I've just written an article that

Is there a stopwatch in Java?

非 Y 不嫁゛ 提交于 2019-11-26 08:13:43
问题 Is there a stopwatch in Java? On google I only find code of stopwatches which don\'t work - they always return 0 milliseconds. This code that I found doesn\'t work but I don\'t see why. public class StopWatch { private long startTime = 0; private long stopTime = 0; private boolean running = false; public void start() { this.startTime = System.currentTimeMillis(); this.running = true; } public void stop() { this.stopTime = System.currentTimeMillis(); this.running = false; } //elaspsed time in

How accurate is Thread.Sleep(TimeSpan)?

ぃ、小莉子 提交于 2019-11-26 04:53:04
问题 I\'ve come across a unit test that is failing intermittently because the time elapsed isn\'t what I expect it to be. An example of what this test looks like is: Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); TimeSpan oneSecond = new TimeSpan(0, 0, 1); for(int i=0; i<3; i++) { Thread.Sleep(oneSecond); } stopwatch.Stop(); Assert.GreaterOrEqual(stopwatch.ElapsedMilliseconds, 2999); Most of the time this passes but it has failed on at least on one occasion failed because: Expected:

Calculate the execution time of a method

大兔子大兔子 提交于 2019-11-26 03:16:08
问题 Possible Duplicate: How do I measure how long a function is running? I have an I/O time-taking method which copies data from a location to another. What\'s the best and most real way of calculating the execution time? Thread ? Timer ? Stopwatch ? Any other solution? I want the most exact one, and briefest as much as possible. 回答1: Stopwatch is designed for this purpose and is one of the best ways to measure time execution in .NET. var watch = System.Diagnostics.Stopwatch.StartNew(); // the

Stopwatch vs. using System.DateTime.Now for timing events [duplicate]

大兔子大兔子 提交于 2019-11-26 02:37:25
问题 This question already has answers here : Is DateTime.Now the best way to measure a function's performance? (15 answers) Closed 6 years ago . I wanted to track the performance of my code so I stored the start and end time using System.DateTime.Now . I took the difference between the two as the time my code to execute. I noticed though that the difference didn\'t appear to be accurate. So I tried using a Stopwatch object. This turned out to be much, much more accurate. Can anyone tell me why