views:

698

answers:

10

I've not be coding long so I'm not familiar with which technique is quickest so I was wondering if there was a way to do this in VS or with a 3rd party tool?

Thanks

+1  A: 

Use a profiler. ANTS costs money but is very nice.

Andrew Hare
+6  A: 

Profiling.

RedGate has a product.
JetBrains has a product.

Larsenal
A: 

ANTS Profiler is very good.

Nathan W
A: 

Tutorial/Explanation of ANTS Profiler

Ric Tokyo
A: 

If you don't want to pay, the newer VS verions come with a profiler, but to be honest it doesn't seem very good. ATI/AMD make a free profiler... but its not very user friendly (to me, I couldn't get any useful info out of it).

The advice I would give is to time function calls yourself with code. If they are fast and you do not have a high-precision timer or the calls vary in slowness for a number of reasons (e.g. every x calls building some kind of cache), try running each one x10000 times or something, then dividing the result accordingly. This may not be perfect for some sections of code, but if you are unable to find a good, free, 3rd party solution, its pretty much what's left unless you want to pay.

jheriko
A: 

I've used ANTS Profiler and I can join the others with recommendation.

The price is NEGLIGIBLE when you compare it with the amount of dev hours it will save you.

I you're developer for a living, and your company won't buy it for you, either change the company or buy it for yourself.

muerte
A: 

Yet another option is Intel's VTune.

DocMax
+1  A: 

For profiling large complex UI applications then you often need a set of tools and approaches. I'll outline the approach and tools I used recently on a project to improve the performance of a .Net 2.0 UI application.

First of all I interviewed users and worked through the use cases myself to come up with a list of target use cases that highlighted the systems worse performing areas. I.e. I didn't want to spend n man days optimising a feature that was hardly ever used but very slow. I would want to spend time, however, optimising a feature that was a little bit sluggish but invoked a 1000 times a day, etc.

Once the candidate use cases were identified I instrumented my code with my own light weight logging class (I used some high performance timers and a custom logging solution because a needed sub-millisecond accuracy). You might, however, be able to get away with log4net and time stamps. The reason I instrumented code is that it is sometimes easier to read your own logs rather than the profiler's output. I needed both for a variety of reasons (e.g. measuring .Net user control layouts is not always straightforward using the profiler).

I then ran my instrumented code with the ANTS profiler and profiled the use case. By combining the ANTS profile and my own log files I was very quickly able to discover problems with our application.

We also profiled the server as well as the UI and were able to work out breakdowns for time spent in the UI, time spent on the wire, time spent on the server etc.

Also worth noting is that 1 run isn't enough, and the 1st run is usually worth throwing away. Let me explain: PC load, network traffic, JIT compilation status etc can all affect the time a particular operation will take. A simple strategy is to measure an operation n times (say 5), throw away the slowest and fastest run, the analyse the remianing profiles.

ng5000
I think you're a little too happy to throw away there. I'd take at least tens of results before even considering throwing away. Usually when I do perf analysis I take Min / Max / Avg. I then present the results with and without the first run (to isolate initialisation and JIT)
Quibblesome
+4  A: 

OK, downvote time...

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.

Mike Dunlavey
I completely agree with this Mike. I would suggest you edit this and just post the "To answer your question" part.
Mo Flanagan
@Mo: Thanks - I'll consider it. It's just that I get so frustrated that the whole industry is so blind on this particular issue, while being so intelligent on other things.
Mike Dunlavey
+2  A: 

Eqatec profiler is a cute small profiler that is free and easy to use. It probably won't come anywhere near the "wow" factor of Ants profiler in terms of features but it still is very cool IMO and worth a look.

Quibblesome