views:

395

answers:

8

I'm writing a plug-in for another program in C#.NET, and am having performance issues where commands take a lot longer then I would. The plug-in reacts to events in the host program, and also depends on utility methods of the the host program SDK. My plug-in has a lot of recursive functions because I'm doing a lot of reading and writing to a tree structure. Plus I have a lot of event subscriptions between my plugin and the host application, as well as event subscriptions between classes in my plug-in.

How can I figure out what is taking so long for a task to complete? I can't use regular breakpoint style debugging, because it's not that it doesn't work it's just that it's too slow. I have setup a static "LogWriter" class that I can reference from all my classes that will allow me to write out timestamped lines to a log file from my code. Is there another way? Does visual studio keep some kind of timestamped log that I could use instead? Is there someway to view the call stack after the application has closed?

+1  A: 

Sounds like you want a code 'profiler'. http://en.wikipedia.org/wiki/Code_profiler#Use_of_profilers

I'm unfamiliar with which profilers are the best for C#, but I came across this link after a quick google which has a list of free open-source offerings. I'm sure someone else will know which ones are worth considering :)

http://csharp-source.net/open-source/profilers

CapBBeard
+12  A: 

You need to use profiler. Here link to good one: ANTS Performance Profiler.

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.

arbiter
Red Gate sure does make good software. Their ANTS performance profiler is one of the best in my opinion. I believe you can download a trial version. Most of the people who do buy the real version afterwards, so be warned :).
bastijn
Well Microsoft profiler is also good one, but it is included only in expensive VS packs.
arbiter
Thumbs up for ANTS from me too, resolved a huge memory leak for me
Simon Wilson
@Simon Wilson, you are talking about other ants product - memory profiler :)
arbiter
+1 on the ANTS profiler. Seriously. You WILL find your performance bottleneck with that tool. Well worth the cost of the product.
Brian Genisio
Thanks for all the responses. Debug.Write seems the simplest for me right now. I'm defitely going to check out DebugView though, the highlighting features should make reading the log a lot easier.
Eric Anastas
+1  A: 

Despite the title of this topic I must argue that the "best" way is subjective, we can only suggest possible solutions.

I have had experience using Redgate ANTS Performance Profiler which will show you where the bottlenecks are in your application. It's definitely worth checking out.

Nippysaurus
+4  A: 

My Profiler List includes ANTS, dotTrace, and AQtime.


However, looking more closely at your question, it seems to me that you should do some unit testing at the same time you're doing profiling. Maybe start by doing a quick overall performance scan, just to see which areas need most attention. Then start writing some unit tests for those areas. You can then run the profiler while running those unit tests, so that you'll get consistent results.

John Saunders
A: 

Don't ever forget Rico Mariani's advice on how to carry out a good perf investigation.

Ariel
+1  A: 

Visual Studio Team System has a profiler baked in, its far from perfect, but for simple applications you can kind of get it to work.

Recently I have had the most success with EQATECs free profiler, or rolling my own tiny profiling class where needed.

Also, there have been quite a few questions about profilers in that past see: http://www.google.com.au/search?hl=en&q=site:stackoverflow.com+.net+profiler&btnG=Google+Search&meta=&aq=f&oq=

Sam Saffron
+1 for pointing to the profiler that new to me.
arbiter
A: 

You can also use performance counter for asp.net applications.

jalpesh
+2  A: 

In my experience, the best method is also the simplest. Get it running, and while it is being slow, hit the "pause" button in the IDE. Then make a record of the call stack. Repeat this several times. (Here's a more detailed example and explanation.)

What you are looking for is any statement that appears on more than one stack sample that isn't strictly necessary. The more samples it appears on, the more time it takes. The way to tell if the statement is necessary is to look up the stack, because that tells you why it is being done.

Anything that causes a significant amount of time to be consumed will be revealed by this method, and recursion does not bother it.

People seem to tackle problems like this in one of two ways:

  1. Try to get good measurements before doing anything.

  2. Just find something big that you can get rid of, rip it out, and repeat.

I prefer the latter, because it's fast, and because you don't have to know precisely how big a tumor is to know it's big enough to remove. What you do need to know is exactly where it is, and that's what this method tells you.

Mike Dunlavey