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

2
  • 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. Commented Jan 24, 2011 at 13:26
  • Release mode will not typically change the execution time of data access. Commented Jan 24, 2011 at 13:33

5 Answers 5

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"); } 
Sign up to request clarification or add additional context in comments.

Comments

0

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

but if you can, try ANTS Performance Profiler

Comments

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.

Comments

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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.