I want to analyse the execution time of method Tuple<A,B,C,D> Calculate()
.
What I did is the following:
Tuple<A, B, C, D, E>
Calculate(Line l, Path p)
{
var stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
var rv = Calculate2(l, p);
System.Diagnostics.Debug.WriteLine($" stopwatch.ElapsedMilliseconds} ms;");
}
return rv;
}
Tuple<A, B, C, D, E>
Calculate2(Line l, Path p)
{
var stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
A a = ...; B b = ...; C c = ...; D d = ...; E e = ...
... content of this method
System.Diagnostics.Debug.WriteLine($" stopwatch.ElapsedMilliseconds} ms;");
return Tuple.Create(a,b,c,d,e);
}
The first measurement gives 28 ms, the second one only 8 ms (every time).
Does this mean that the return statement
takes 20 ms?
I find this impossible to believe..?
It is not a Tuple
-issue: I tried a struct tuple and an explicit class, with the exact same result.
EDIT
I added a static stopwatch in the class and found that immediately on entrance of Calculate2(..)
the static stopwatch shows a 20 ms delay.
I wonder whether this might have to do with JIT compliation of the method.
I added a second run after the first and it didn't show a delay.
As a matter of fact the delay was in the first call to that function and for some other reason I assumed this first one was the bottleneck.