views:

733

answers:

9

I'm a total newbie, but I was writing a little program that worked on strings in C# and I noticed that if I did a few things differently, the code executed significantly faster.

So it had me wondering, how do you go about clocking your code's execution speed? Are there any (free)utilities? Do you go about it the old-fashioned way with a System.Timer and do it yourself?

+1  A: 

Use a profiler.

If you need to time one specific method only, the Stopwatch class might be a good choice.

Lars A. Brekken
A: 

I do the following things: 1) I use ticks (e.g. in VB.Net Now.ticks) for measuring the current time. I subtract the starting ticks from the finished ticks value and divide by TimeSpan.TicksPerSecond to get how many seconds it took. 2) I avoid UI operations (like console.writeline). 3) I run the code over a substantial loop (like 100,000 iterations) to factor out usage / OS variables as best as I can.

torial
+11  A: 
spoon16
We use this. It is very effective at finding the one routine that takes 95% of the time!
Christian Payne
A: 

You can use the StopWatch class to time methods. Remember the first time is often slow due to code having to be jitted.

Chris
+9  A: 

What you are describing is known as performance profiling. There are many programs you can get to do this such as Jetbrains or Ants profiler, although most will slow down your application whilst in the process of measuring its performance.

To hand-roll your own performance profiling, you can use System.Diagnostics.StopWatch and a simple Console.WriteLine, like you described.

Also keep in mind that the C# jit compiler optimizes code depending on the type and frequency it is called, so play around with loops of differing sizes and methods such as recursive calls to get a feel of what works best.

Xian
+1: That stopwatch is a handy little utility for quick checks - thanks!
Jon Cage
+5  A: 

Just a reminder - make sure to compile in Relase, not Debug! (I've seen this mistake made by seasoned developers - it's easy to forget).

ripper234
+1  A: 

What are you describing is 'Performance Tuning'. When we talk about performance tuning there are two angle to it. (a) Response time - how long it take to execute a particular request/program. (b) Throughput - How many requests it can execute in a second. When we typically 'optimize' - when we eliminate unnecessary processing both response time as well as throughput improves. However if you have wait events in you code (like Thread.sleep(), I/O wait etc) your response time is affected however throughput is not affected. By adopting parallel processing (spawning multiple threads) we can improve response time but throughput will not be improved. Typically for server side application both response time and throughput are important. For desktop applications (like IDE) throughput is not important only response time is important.

You can measure response time by 'Performance Testing' - you just note down the response time for all key transactions. You can measure the throughput by 'Load Testing' - You need to pump requests continuously from sufficiently large number of threads/clients such that the CPU usage of server machine is 80-90%. When we pump request we need to maintain the ratio between different transactions (called transaction mix) - for eg: in a reservation system there will be 10 booking for every 100 search. there will be one cancellation for every 10 booking etc.

After identifying the transactions require tuning for response time (performance testing) you can identify the hot spots by using a profiler. You can identify the hot spots for throughput by comparing the response time * fraction of that transaction. Assume in search, booking, cancellation scenario, ratio is 89:10:1. Response time are 0.1 sec, 10 sec and 15 sec. load for search - 0.1 * .89 = 0.089 load for booking- 10 * .1 = 1 load for cancell= 15 * .01= 0.15 Here tuning booking will yield maximum impact on throughput. You can also identify hot spots for throughput by taking thread dumps (in the case of java based applications) repeatedly.

Rejeev Divakaran
A: 

There is a native .NET option (Team Edition for Software Developers) that might address some performance analysis needs. From the 2005 .NET IDE menu, select Tools->Performance Tools->Performance Wizard...

[GSS is probably correct that you must have Team Edition]

e-holder
A: 

^I think that is only for one of the specif versions of Visual Studio 2005 (Database Designer or Software Engineer, not sure).

dotnetdev