Programme performance
I have a particular object that is really busy with data access from external sources and as such it taks a while to instantiate. Is there anyway i can measure the time of method calls within its contructor开发者_Python百科 to see which is really doing the damage?
Thanks
You can use the StopWatch class to measure the time in your constructor.
Example :
public Class1()
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// Do your stuff here...
stopWatch.Stop();
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine(elapsedTime, "RunTime");
}
i like to use stopwatch for quick review, see this question for some info
but if you can, try ANTS Performance Profiler
As others say, use Stopwatch class. However, a good method for using this is to run your test procedure once before starting the stopwatch, to get everything JITted, cached, etc. Then within the timed section, run the test 1000 times to get an average. Repeat the whole business several times to get averages, since timing is always variable.
You could use the Performance tools in Visual Studio (if you have the correct version). That way you don't need to write code and you also are able to pin-point the time consuming metods better.
If you want to see how long methods are taking, Eqatec profiler is free. I use it for that purpose.
Since you mention data access, you might want to look at sql profiler (Assuming MS Sql Server).
精彩评论