Wednesday, April 17, 2013

To monitor detect slow performance of C# .NET framework application

You need to use profilers:
- Redgate ANTS Performance Profiler
- CLR Profiler for .NET Framework 4
- Visual Studio 2010 Performance Tools with Service Pack 1

Update: You can also write messages in control points using Debug.Write. Then you need to load DebugView application that displays all your debug string with precise time stamp. It is freeware and very good for quick debugging and profiling.

Profilers are great for measuring.

But your question was "How can I determine where the slow parts of my code are?".

That is a different problem. It is diagnosis, not measurement.

I know this is not a popular view, but it's true.

It is like a business that is trying to cut costs.

One approach (top down) is to measure the overall finances, then break it down by categories and departments, and try to guess what could be eliminated. That is measurement.

Another approach (bottom up) is to walk in at random into an office, pick someone at random, and ask them what they are doing at that moment and (importantly) why, in detail.

Do this more than once.

That is what Harry Truman did at the outbreak of WW2, in the US defense industry, and immediately uncovered massive fraud and waste, by visiting several sites. That is diagnosis.

In code you can do this in a very simple way: "Pause" it and ask it why it is spending that particular cycle. Usually the call stack tells you why, in detail.

Do this more than once.

This is sampling. Some profilers sample the call stack. But then for some reason they insist on summarizing time spent in each function, inclusive and exclusive. That is like summarizing by department in business, inclusive and exclusive.

It loses the information you need, which is the fine-grain detail that tells if the cycles are necessary.

To answer your question:

Just pause your program several times, and capture the call stack each time. If your code is very slow, the wasteful function calls will be on nearly every stack. They will point with precision to the "slow parts of your code".

ADDED: RedGate ANTS is getting there. It can give you cost-by-line, and it is quite spiffy. So if you're in .NET, and can spare 3 figures, and don't mind waiting around to install & learn it, it can tell you much of what your Pause key can tell you, and be much more pretty about it.


No comments: