Reputation: 5001
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 to see which is really doing the damage?
Thanks
Upvotes: 1
Views: 104
Reputation: 110221
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).
Upvotes: 0
Reputation: 3205
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");
}
Upvotes: 2
Reputation: 50712
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.
Upvotes: 0
Reputation: 7211
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.
Upvotes: 0
Reputation: 20140
i like to use stopwatch for quick review, see this question for some info
but if you can, try ANTS Performance Profiler
Upvotes: 0