1

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

tom
  • 4,911
  • 11
  • 37
  • 39
  • Please note that you should always do performance testing with your program compiled in **Release** mode. There are plenty of questions here on Stack Overflow that testify this is not common knowledge. – Cody Gray - on strike Jan 24 '11 at 13:26
  • Release mode will not typically change the execution time of data access. – Amy B Jan 24 '11 at 13:33

5 Answers5

2

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");
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Shimrod
  • 3,115
  • 2
  • 36
  • 56
0

i like to use stopwatch for quick review, see this question for some info

but if you can, try ANTS Performance Profiler

Community
  • 1
  • 1
Fredou
  • 19,848
  • 10
  • 58
  • 113
0

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.

Cylon Cat
  • 7,111
  • 2
  • 25
  • 33
0

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.

Emond
  • 50,210
  • 11
  • 84
  • 115
0

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).

Amy B
  • 108,202
  • 21
  • 135
  • 185