I declared the following variables:
private Stopwatch stopwatch;
private long t1, t2, t3, t4, t5;
and I am using Stopwatch like this:
try {
stopwatch = Stopwatch.StartNew();
get1();
t1 = stopwatch.ElapsedMilliseconds;
anotherGet();
t2 = stopwatch.ElapsedMilliseconds;
vm.Detail = anotherAnotherGet();
t3 = stopwatch.ElapsedMilliseconds;
} catch (Exception e) {
log(e);
} finally {
Stopwatch.Stop();
if (vm.Detail.Count() > 0) {
return PartialView("Details", vm);
} else {
return Content("No records found");
}
}
Is there a better way I could store my times rather than in
longs named 1-5. The problem is that in some areas of
code I might only need t1 and in other areas I might need
more than 5 timing points.
I am using a finally for stopping. Is that the correct way to
do this?
You could use a Dictionary so you can store the values under a name/key.
Beware that adding the values to a collection might cost some time that could influence your measurements.
So you might want to do this:
The finally is OK