Reputation: 523
this will be a follow up question from a previous one i made: Stopwatch different timing why?
i am using stopwatch inside a web service, only to know the time that takes some methods i have program something like this: (program in C#, using ASP.NET)
Stopwatch swK=new Stopwatch();
swK.Start();
do
{
method1(intvar1,intvar2,intvar3);
var_1=method2(intvar1,doublevar1, doublevar2);
flag= Compara (doublevar3,intvar1,doublevar4);
}
while (flag != 1);
swK.Stop();
double tim2 = swK.Elapsed.TotalMilliseconds;
this way tim2 will have the time in milliseconds take from all the methods inside the 'while';what i dont understand is way the first time it still take to much time for example the firstime it take 10 milliseconds the next time it only take 1 milliseconds,
so my question is way if i am only mesuaring the time of proces of some methods it takes so much time the first time?, is it becaus of Assemblies, the jiiter etc...
and also is their another way to measure my methods whit out been afecte by some other factors?
Upvotes: 0
Views: 661
Reputation: 2817
The most likely cause for it to take a long time the first time you run it is for the Just In Time compilation. Another cause for this, in the case of web services, is network delays. Usually, the best way to measure the time a procedure takes, is by doing several measures (let's say 10) and get the mean of them, also there are other statistical approaches, but that depends of the level of sofistication you want to get for this measures.
Upvotes: 0
Reputation: 73574
In .NET, there are plenty of things that take a while the first time, and are quicker on subsequent operations. Connecting to a database, connecting to a Web service, attaching to a file system, etc. It could even be a matter of JIT compiling happening, particularly if you're changing code and re-running it. Or even just how the runtime is allocating and deallocating objects.
There is a lot of "plumbing" going on behind the scenes that you don't even need to think about as a .NET developer. And there are optimizations like Connection Pooling that speed things up on subsequent attempts without you even realizing it.
As your question is phrased, it could be any of those types of things. We can't necessarily answer this definitively here. We have no idea what's going on inside those methods, but hopefully the above sheds some light on what could be happening to cause this.
Toi narrow it down, in Visual Studio (if you have the right version) you can use profiling to try to find bottlenecks.
Upvotes: 3