I want to measure the time between fucnctions+ get the total time
I now that there is Stopwatch but what I know is that I can only get the time between start and stop with Elapsed , so on that way I can get the total time.
How can I get the time between functions? for ex. :
Stopwatch s= new Stopwatch();
s.Start();
Function1();
//here I want get the time of function1
Function2();
//here I want get the time of function2
Function3();
//here I want get the time of function3
//here I want get the time of total time
s.Stop();
If I restart the stopwatch between the function I will not have the total time.
I don't want to apply more than 1 stopwatch
What can I do?
You can retrieve the elapsed time from the Stopwatch as you go:
Stopwatch s= new Stopwatch();
s.Start();
Function1();
var timeF1 = s.Elapsed;
Function2();
var timeF2 = s.Elapsed-timeF1;
Function3();
var timeF3 = s.Elapsed-timeF2-timeF1;
s.Stop();
//here I want get the time of total time
var timeTotal = s.Elapsed;
You could make yourself a small helper that stops it for you and returns you the elapsed times:
using System;
using System.Threading;
using System.Diagnostics;
public class Program
{
// measures a given lambdas run time - you can call any
// other function by StopFunc( () => ......... ) and supply
// your function call instead of ........
public static TimeSpan StopFunc(Action act)
{
var watch = Stopwatch.StartNew();
act?.Invoke(); // call the function
watch.Stop();
return watch.Elapsed;
}
static void Fast() { Thread.Sleep(100); }
static void Slow() { Thread.Sleep(2000); }
public static void Main()
{
// this calls the "to be measured function"
// via lambda and prints the time needed
Console.WriteLine("Slow took: " + StopFunc( () => Slow()));
Console.WriteLine("Fast took: " + StopFunc( () => Fast()));
}
}
Output:
Slow took: 00:00:02.0024322
Fast took: 00:00:00.1099702
The StopFunc takes an Action which you supply by a lambda ( () => YourFuncToCall() ) - it is just for measurements though.
Here is a class I've used in the past that may be useful to you. It provides a simple abstraction over the Stopwatch class which allows you to keep your code as free as possible of boiler plate code needed to do the timing. This makes it easier to read and easier to remove at a later stage.
public class StopwatchWrapper : IDisposable
{
private bool disposed = false;
private Stopwatch _overallStopwatch;
private List<long> _increments;
public StopwatchWrapper()
{
_overallStopwatch = Stopwatch.StartNew();
_increments = new List<long>();
}
public void Reset()
{
_increments.Clear();
_overallStopwatch.Restart();
}
public long ElapsedMilliseconds
{
get
{
_overallStopwatch.Stop();
var elapsed = _overallStopwatch.ElapsedMilliseconds;
_increments.Add(elapsed);
_overallStopwatch.Start();
return elapsed;
}
}
public long OverallMilliseconds
{
get
{
return _increments.Sum();
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if (_overallStopwatch != null)
{
_overallStopwatch.Stop();
}
}
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
}
}
You could use it in your scenario as follows:
using (var sw = new StopwatchWrapper())
{
Function1();
//here I want get the time of function1
Console.WriteLine($"{sw.ElapsedMilliseconds}ms");
Function2();
//here I want get the time of function2
Console.WriteLine($"{sw.ElapsedMilliseconds}ms");
Function3();
//here I want get the time of function3
Console.WriteLine($"{sw.ElapsedMilliseconds}ms");
//here I want get the time of total time
Console.WriteLine($"{sw.OverallMilliseconds}ms");
}
Related
I'm learning async in C# and want to show a program runtime every three seconds. I have two solutions, but neither works completely properly.
Solution 1
In the first solution, I have a loop where I call two methods. The first performs the calculation and the second shows the startup time, which is divisible by 3.
namespace Async
{
class Program
{
static void Main(string[] args)
{
PerformLoop();
Console.ReadLine();
}
public static async void PerformLoop()
{
Stopwatch timer = new Stopwatch();
timer.Start();
List<Task> l = new List<Task>();
for (int i = 0; i < 50; i++)
{
l.Add(AsyncCalculation(i));
l.Add(ShowTime(Convert.ToInt32(timer.Elapsed.TotalMilliseconds)));
}
await Task.WhenAll(l);
timer.Stop();
Console.WriteLine("Total execution time: " +
timer.Elapsed.TotalMilliseconds);
}
public async static Task AsyncCalculation(int i)
{
var result = 10 * i;
Console.WriteLine("Calculation result: " + result);
}
public async static Task ShowTime(int execTime)
{
if (execTime % 3 == 0)
{
Console.WriteLine("Execution time: " + execTime);
}
}
}
}
Solution 2
In the second solution, I call two methods in a loop. The first performs calculations and the second displays the operation time after 3 seconds. Unfortunately, in this case, the second method blocks the execution of the first.
namespace Async
{
class Program
{
static void Main(string[] args)
{
CallMethod();
Console.ReadLine();
}
public static async void CallMethod()
{
for (int i = 0; i < 50; i++)
{
var results = Calculation(i);
var calcResult = results.Item1;
var time = results.Item2;
ShowResult(calcResult);
await ShowDelayTime(time);
}
}
public static void ShowResult(int calcResult)
{
Console.WriteLine("Calculation result: " + calcResult);
}
public async static Task ShowDelayTime(int execTime)
{
await Task.Delay(3000);
Console.WriteLine("Execution time: " + execTime);
}
public static Tuple<int, int> Calculation(int i)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var result = 10 * i;
stopwatch.Stop();
return Tuple.Create(result,
Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds));
}
}
}
I have no idea how to continuously display the calculation results and show the running time of the program by three seconds :(((
Edit
Expected output (example):
Calculation result: 0
Calculation result: 10
Execution time: 3 seconds
Calculation result: 20
Calculation result: 30
Calculation result: 40
Execution time: 6 seconds
Calcultion result: 50
//Next iterations
The program now shows the result, waits three seconds, and then go to next iteration. I want iterations for calculations to show regardless (independently) of time. I want the time to show every three seconds of the program running.
You could use a System.Threading.Timer in order to invoke a callback every 3 seconds, and a Stopwatch in order to measure the elapsed seconds since the start of the program:
var stopwatch = Stopwatch.StartNew();
var timer = new System.Threading.Timer(_ =>
{
Console.WriteLine($"Execution time: {stopwatch.Elapsed.TotalSeconds:#,0} seconds");
}, null, 3000, 3000);
I have console app which performs a couple of tasks in parallel. In my case the important part is that i want the tasks to complete at the same time. I know how long each task is going to take. So the idea is to delay somehow every task in the Parallel.ForEach with custom time delay per task so they all finish at the same time and in the end all the tasks will complete approximately at the same time as the one which takes most time.
Example:
interface IService
{
void DoWork();
}
class Program
{
static void Main(string[] args)
{
var services = new List<IService>();
services.Add(new ServiceA());//Takes 500ms to DoWork()
services.Add(new ServiceB());//Takes 1000ms to DoWork()
services.Add(new ServiceC());//Takes 5000ms to DoWork()
Parallel.ForEach(services, z =>
{
Stopwatch sw = new Stopwatch();
sw.Start();
z.DoWork();
Console.WriteLine($"Ready for {sw.Elapsed}");
});
}
}
Output:
Ready for 00:00:00.5006837
Ready for 00:00:01.0002284
Ready for 00:00:05.0010202
Press any key to continue . . .
How to modify the code so the output will look like:
Ready for 00:00:05.5006837
Ready for 00:00:05.0002284
Ready for 00:00:05.0010202
Press any key to continue . . .
I guess the most obvious solution would be to distinguish which Service is z in the loop and add custom Thread.Sleep before calling z.DoWork(), but I am looking for smarter solution.
Thanks for the help! So this is what i wanted to achieve. All the services finish their job at the same time. Is this the right way of doing it?
class Program
{
static void Main(string[] args)
{
ServiceA a = new ServiceA();
ServiceB b = new ServiceB();
ServiceC c = new ServiceC();
int maxDelay = 3000;
var sw = new Stopwatch();
sw.Start();
Task taskA = Task.Delay(maxDelay - a.ResponseTime).ContinueWith(x =>
{
a.DoWork();
Console.WriteLine($"taskA ready for {sw.Elapsed}");
});
Task taskB = Task.Delay(maxDelay - b.ResponseTime).ContinueWith(x =>
{
b.DoWork();
Console.WriteLine($"taskB ready for {sw.Elapsed}");
});
Task taskC = Task.Delay(maxDelay - c.ResponseTime).ContinueWith(x =>
{
c.DoWork();
Console.WriteLine($"taskC ready for {sw.Elapsed}");
});
taskA.Wait();
taskB.Wait();
taskC.Wait();
Console.WriteLine(sw.Elapsed);
}
}
class ServiceA
{
public int ResponseTime { get => 500; }
public void DoWork() => Thread.Sleep(500);
}
class ServiceB
{
public int ResponseTime { get => 1000; }
public void DoWork() => Thread.Sleep(1000);
}
class ServiceC
{
public int ResponseTime { get => 3000; }
public void DoWork() => Thread.Sleep(3000);
}
Output:
taskA ready for 00:00:03.0084344
taskC ready for 00:00:03.0102184
taskB ready for 00:00:03.0102588
*Thread.Sleep(x) represents some time consuming operations.
how can I record the time duration, time start and time end of a method once it was executed using c#?
for example, I click a button and it will do something. Once it start, I'll get the start time, then when the execution is done, I'll get the time end and also the duration of time it take to finish.
You can use the Stopwatch, which resides in System.Diagnostics namespace.
This has the features of a normal stopwatch, with Start, Stop, Reset, ElapsedMilliseconds and so forth.
This is great for measuring a specific code block or method. You do however state that you want both start and end time in addition to the duration of execution. You could create a custom stopwatch by inheriting the Stopwatch class and extending it with a couple of DateTime properties.
public class CustomStopwatch : Stopwatch
{
public DateTime? StartAt { get; private set; }
public DateTime? EndAt { get; private set; }
public void Start()
{
StartAt = DateTime.Now;
base.Start();
}
public void Stop()
{
EndAt = DateTime.Now;
base.Stop();
}
public void Reset()
{
StartAt = null;
EndAt = null;
base.Reset();
}
public void Restart()
{
StartAt = DateTime.Now;
EndAt = null;
base.Restart();
}
}
And use it like this:
CustomStopwatch sw = new CustomStopwatch();
sw.Start();
Thread.Sleep(2342); // just to use some time, logic would be in here somewhere.
sw.Stop();
Console.WriteLine("Stopwatch elapsed: {0}, StartAt: {1}, EndAt: {2}", sw.ElapsedMilliseconds, sw.StartAt.Value, sw.EndAt.Value);
You can use System.Diagnostics.Stopwatch class to achieve this. see the sample
// Create new stopwatch
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
// Begin timing
stopwatch.Start();
// Tasks performed by method
// Stop timing
stopwatch.Stop();
Console.WriteLine("Time taken : {0}", stopwatch.Elapsed);
I've done it by doing this
var watch = System.Diagnostics.Stopwatch.StartNew();
string startTime = DateTime.Now.ToLongTimeString();
//Insert Code Here
watch.Stop();
string timeEnd = DateTime.Now.ToLongTimeString();
//Time Format
string[] hours = watch.Elapsed.TotalHours.ToString().Split('.');
string[] minutes = watch.Elapsed.TotalMinutes.ToString().Split('.');
string[] seconds = watch.Elapsed.TotalSeconds.ToString().Split('.');
string[] milliseconds = watch.Elapsed.TotalMilliseconds.ToString().Split('.');
MessageBox.Show(hours[0].ToString() + ":" + minutes[0].ToString() + ":" + seconds[0].ToString() + "." + milliseconds[0].ToString());
First, you need to create a stopwatch object.
private readonly Stopwatch stopwatch = new Stopwatch();
Then, your method:
public void MyMethod()
{
stopwatch.Start();
// Any other code here.
stopwatch.Stop();
//returns longs
long runningTimeInMs = stopwatch.ElapsedMilliseconds;
}
I'm writing a small lap counter for slot car races as a little home project. I want to implement a countdown timer, which I've done with the following as a test:
private Thread countdownThread;
private delegate void UpdateTimer(string update);
UpdateTimer ut;
public LapCounterForm()
{
InitializeComponent();
//...
ut += updateTimer;
countdownThread = new Thread(new ThreadStart(startCountdown));
}
private void startCountdown()
{
Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(1);
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
long time = 0;
stopwatch.Start();
while (stopwatch.ElapsedMilliseconds <= 5000)
{
time = 5000 - stopwatch.ElapsedMilliseconds;
TimeSpan ts = TimeSpan.FromMilliseconds(time);
ut(ts.Minutes.ToString().PadLeft(2, '0') + ":" + ts.Seconds.ToString().PadLeft(2, '0') + ":" + ts.Milliseconds.ToString().PadLeft(3, '0'));
}
}
private void updateTimer(string text)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<String>(ut), new object[] { text });
}
else
{
lblCountdownClock.Text = text;
}
}
When I start my thread, it works. I get my 5 second countdown like I want, but I can see that I'm using a lot of CPU in the process (12% of my 8 thread i7 2600k).
I figure I can reduce this load a lot by only updating the UI every 10 milliseconds instead of every millisecond, but I have no idea how to do such, other than using if(time % 10 == 0) before making the TimeSpan and updating the UI but I suspect that will be just as inefficient thanks to the while loop.
Am I reinventing the wheel? I'd like my timer to be as accurate as possible (at least for the slot car lap time recordings, perhaps the UI does not need to be updated so often).
EDIT: I tried commenting out the actual string manipulation and UI update as suggested in the comments. Now when I start my thread my entire UI hangs until the thread exits and I still get 12% CPU usage. I suspect that while loop is eating up a lot of CPU time.
Update: I went with the multimedia timer (here) posted by Kohanz as well as Daniel's answer. I no longer use another thread at all, I just make one of those timer objects and have a tick event handler calculating the time between clicking the start button and the tick event. I can even set the period for my ticks to 1ms so I get my cool looking countdown, and it's apparently using 0% CPU :) I'm quite happy with this.
Dont, just DONT go down this road. You are completely thinking of this in the wrong way. You are basically forcing your thread to freeze for no benefit.
Basically any game works this way: you have an update loop, and whenever that triggers you do neccessary stuff. So for instance if you want to know how much time, you ask some kind of "timer" how much has passed since something happened
Here's a much better way to handle this:
class MyStopwatch {
private DateTime _startTime;
private DateTime _stopTime;
public void start() {
_running = true;
_startTime = DateTime.Now;
}
public void stop() {
_stopTime = DateTime.Now;
_running = false;
}
public double getTimePassed() {
if(_running) {
return (DateTime.Now - _startTime).TotalMilliseconds;
} else {
return (_stopTime - _startTime).TotalMilliseconds;
}
}
}
A bit after the fact, but this shows a way in which you might achieve what you need:
public class LapTimer : IDisposable
{
private readonly System.Diagnostics.Stopwatch _stopWatch = new System.Diagnostics.Stopwatch();
private readonly ConcurrentDictionary<string, List<TimeSpan>> _carLapTimes = new ConcurrentDictionary<string, List<TimeSpan>>();
private readonly Action<TimeSpan> _countdownReportingDelegate;
private readonly TimeSpan _countdownReportingInterval;
private System.Threading.Timer _countDownTimer;
private TimeSpan _countdownTo = TimeSpan.FromSeconds(5);
public LapTimer(TimeSpan countdownReportingInterval, Action<TimeSpan> countdownReporter)
{
_countdownReportingInterval = countdownReportingInterval;
_countdownReportingDelegate = countdownReporter;
}
public void StartRace(TimeSpan countdownTo)
{
_carLapTimes.Clear();
_stopWatch.Restart();
_countdownTo = countdownTo;
_countDownTimer = new System.Threading.Timer(this.CountdownTimerCallback, null, _countdownReportingInterval, _countdownReportingInterval);
}
public void RaceComplete()
{
_stopWatch.Stop();
_countDownTimer.Dispose();
_countDownTimer = null;
}
public void CarCompletedLap(string carId)
{
var elapsed = _stopWatch.Elapsed;
_carLapTimes.AddOrUpdate(carId, new List<TimeSpan>(new[] { elapsed }), (k, list) => { list.Add(elapsed); return list; });
}
public IEnumerable<TimeSpan> GetLapTimesForCar(string carId)
{
List<TimeSpan> lapTimes = null;
if (_carLapTimes.TryGetValue(carId, out lapTimes))
{
yield return lapTimes[0];
for (int i = 1; i < lapTimes.Count; i++)
yield return lapTimes[i] - lapTimes[i - 1];
}
yield break;
}
private void CountdownTimerCallback(object state)
{
if (_countdownReportingDelegate != null)
_countdownReportingDelegate(_countdownTo - _stopWatch.Elapsed);
}
public void Dispose()
{
if (_countDownTimer != null)
{
_countDownTimer.Dispose();
_countDownTimer = null;
}
}
}
class Program
{
public static void Main(params string[] args)
{
using (var lapTimer = new LapTimer(TimeSpan.FromMilliseconds(100), remaining => Console.WriteLine(remaining)))
{
lapTimer.StartRace(TimeSpan.FromSeconds(5));
System.Threading.Thread.Sleep(2000);
lapTimer.RaceComplete();
}
Console.ReadLine();
}
}
This question already has answers here:
How to measure code performance in .NET?
(18 answers)
Closed 9 years ago.
What is the most exact way of seeing how long something, for example a method call, took in code?
The easiest and quickest I would guess is this:
DateTime start = DateTime.Now;
{
// Do some work
}
TimeSpan timeItTook = DateTime.Now - start;
But how exact is this? Are there better ways?
A better way is to use the Stopwatch class:
using System.Diagnostics;
// ...
Stopwatch sw = new Stopwatch();
sw.Start();
// ...
sw.Stop();
Console.WriteLine("Elapsed={0}",sw.Elapsed);
As others have said, Stopwatch is a good class to use here. You can wrap it in a helpful method:
public static TimeSpan Time(Action action)
{
Stopwatch stopwatch = Stopwatch.StartNew();
action();
stopwatch.Stop();
return stopwatch.Elapsed;
}
(Note the use of Stopwatch.StartNew(). I prefer this to creating a Stopwatch and then calling Start() in terms of simplicity.) Obviously this incurs the hit of invoking a delegate, but in the vast majority of cases that won't be relevant. You'd then write:
TimeSpan time = StopwatchUtil.Time(() =>
{
// Do some work
});
You could even make an ITimer interface for this, with implementations of StopwatchTimer, CpuTimer etc where available.
As others said, Stopwatch should be the right tool for this. There can be few improvements made to it though, see this thread specifically: Benchmarking small code samples in C#, can this implementation be improved?.
I have seen some useful tips by Thomas Maierhofer here
Basically his code looks like:
//prevent the JIT Compiler from optimizing Fkt calls away
long seed = Environment.TickCount;
//use the second Core/Processor for the test
Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2);
//prevent "Normal" Processes from interrupting Threads
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
//prevent "Normal" Threads from interrupting this thread
Thread.CurrentThread.Priority = ThreadPriority.Highest;
//warm up
method();
var stopwatch = new Stopwatch()
for (int i = 0; i < repetitions; i++)
{
stopwatch.Reset();
stopwatch.Start();
for (int j = 0; j < iterations; j++)
method();
stopwatch.Stop();
print stopwatch.Elapsed.TotalMilliseconds;
}
Another approach is to rely on Process.TotalProcessTime to measure how long the CPU has been kept busy running the very code/process, as shown here This can reflect more real scenario since no other process affects the measurement. It does something like:
var start = Process.GetCurrentProcess().TotalProcessorTime;
method();
var stop = Process.GetCurrentProcess().TotalProcessorTime;
print (end - begin).TotalMilliseconds;
A naked, detailed implementation of the samething can be found here.
I wrote a helper class to perform both in an easy to use manner:
public class Clock
{
interface IStopwatch
{
bool IsRunning { get; }
TimeSpan Elapsed { get; }
void Start();
void Stop();
void Reset();
}
class TimeWatch : IStopwatch
{
Stopwatch stopwatch = new Stopwatch();
public TimeSpan Elapsed
{
get { return stopwatch.Elapsed; }
}
public bool IsRunning
{
get { return stopwatch.IsRunning; }
}
public TimeWatch()
{
if (!Stopwatch.IsHighResolution)
throw new NotSupportedException("Your hardware doesn't support high resolution counter");
//prevent the JIT Compiler from optimizing Fkt calls away
long seed = Environment.TickCount;
//use the second Core/Processor for the test
Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2);
//prevent "Normal" Processes from interrupting Threads
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
//prevent "Normal" Threads from interrupting this thread
Thread.CurrentThread.Priority = ThreadPriority.Highest;
}
public void Start()
{
stopwatch.Start();
}
public void Stop()
{
stopwatch.Stop();
}
public void Reset()
{
stopwatch.Reset();
}
}
class CpuWatch : IStopwatch
{
TimeSpan startTime;
TimeSpan endTime;
bool isRunning;
public TimeSpan Elapsed
{
get
{
if (IsRunning)
throw new NotImplementedException("Getting elapsed span while watch is running is not implemented");
return endTime - startTime;
}
}
public bool IsRunning
{
get { return isRunning; }
}
public void Start()
{
startTime = Process.GetCurrentProcess().TotalProcessorTime;
isRunning = true;
}
public void Stop()
{
endTime = Process.GetCurrentProcess().TotalProcessorTime;
isRunning = false;
}
public void Reset()
{
startTime = TimeSpan.Zero;
endTime = TimeSpan.Zero;
}
}
public static void BenchmarkTime(Action action, int iterations = 10000)
{
Benchmark<TimeWatch>(action, iterations);
}
static void Benchmark<T>(Action action, int iterations) where T : IStopwatch, new()
{
//clean Garbage
GC.Collect();
//wait for the finalizer queue to empty
GC.WaitForPendingFinalizers();
//clean Garbage
GC.Collect();
//warm up
action();
var stopwatch = new T();
var timings = new double[5];
for (int i = 0; i < timings.Length; i++)
{
stopwatch.Reset();
stopwatch.Start();
for (int j = 0; j < iterations; j++)
action();
stopwatch.Stop();
timings[i] = stopwatch.Elapsed.TotalMilliseconds;
print timings[i];
}
print "normalized mean: " + timings.NormalizedMean().ToString();
}
public static void BenchmarkCpu(Action action, int iterations = 10000)
{
Benchmark<CpuWatch>(action, iterations);
}
}
Just call
Clock.BenchmarkTime(() =>
{
//code
}, 10000000);
or
Clock.BenchmarkCpu(() =>
{
//code
}, 10000000);
The last part of the Clock is the tricky part. If you want to display the final timing, its up to you to choose what sort of timing you want. I wrote an extension method NormalizedMean which gives you the mean of the read timings discarding the noise. I mean I calculate the the deviation of each timing from the actual mean, and then I discard the values which was farer (only the slower ones) from the mean of deviation (called absolute deviation; note that its not the often heard standard deviation), and finally return the mean of remaining values. This means, for instance, if timed values are { 1, 2, 3, 2, 100 } (in ms or whatever), it discards 100, and returns the mean of { 1, 2, 3, 2 } which is 2. Or if timings are { 240, 220, 200, 220, 220, 270 }, it discards 270, and returns the mean of { 240, 220, 200, 220, 220 } which is 220.
public static double NormalizedMean(this ICollection<double> values)
{
if (values.Count == 0)
return double.NaN;
var deviations = values.Deviations().ToArray();
var meanDeviation = deviations.Sum(t => Math.Abs(t.Item2)) / values.Count;
return deviations.Where(t => t.Item2 > 0 || Math.Abs(t.Item2) <= meanDeviation).Average(t => t.Item1);
}
public static IEnumerable<Tuple<double, double>> Deviations(this ICollection<double> values)
{
if (values.Count == 0)
yield break;
var avg = values.Average();
foreach (var d in values)
yield return Tuple.Create(d, avg - d);
}
Use the Stopwatch class
System.Diagnostics.Stopwatch is designed for this task.
Stopwatch is fine, but loop the work 10^6 times, then divide by 10^6.
You'll get a lot more precision.
I'm using this:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUrl);
System.Diagnostics.Stopwatch timer = new Stopwatch();
timer.Start();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
statusCode = response.StatusCode.ToString();
response.Close();
timer.Stop();