[ACCEPTED]-C# How can I determine where the slow parts of my code are?-performance
OK, downvote time...
Profilers are great 39 for measuring.
But your question was "How 38 can I determine where the slow parts of 37 my code are?".
That is a different problem. It 36 is diagnosis, not measurement.
I know this is not a popular view, but it's true.
It is like 35 a business that is trying to cut costs.
One 34 approach (top down) is to measure the overall 33 finances, then break it down by categories 32 and departments, and try to guess what could 31 be eliminated. That is measurement.
Another approach (bottom 30 up) is to walk in at random into an office, pick 29 someone at random, and ask them what they 28 are doing at that moment and (importantly) why, in 27 detail.
Do this more than once.
That is what Harry Truman did at 26 the outbreak of WW2, in the US defense industry, and 25 immediately uncovered massive fraud and 24 waste, by visiting several sites. That is diagnosis.
In code 23 you can do this in a very simple way: "Pause" it 22 and ask it why it is spending that particular 21 cycle. Usually the call stack tells you 20 why, in detail.
Do this more than once.
This is sampling. Some profilers 19 sample the call stack. But then for some 18 reason they insist on summarizing time spent 17 in each function, inclusive and exclusive. That 16 is like summarizing by department in business, inclusive 15 and exclusive.
It loses the information you 14 need, which is the fine-grain detail that 13 tells if the cycles are necessary.
To answer your question:
Just pause 12 your program several times, and capture 11 the call stack each time. If your code is 10 very slow, the wasteful function calls will 9 be on nearly every stack. They will point 8 with precision to the "slow parts of your 7 code".
ADDED: RedGate ANTS is getting there. It 6 can give you cost-by-line, and it is quite 5 spiffy. So if you're in .NET, and can spare 4 3 figures, and don't mind waiting around 3 to install & learn it, it can tell you 2 much of what your Pause key can tell you, and 1 be much more pretty about it.
I've used ANTS Profiler and I can join the 6 others with recommendation.
The price is 5 NEGLIGIBLE when you compare it with the 4 amount of dev hours it will save you.
I you're 3 developer for a living, and your company 2 won't buy it for you, either change the 1 company or buy it for yourself.
For profiling large complex UI applications 43 then you often need a set of tools and approaches. I'll 42 outline the approach and tools I used recently 41 on a project to improve the performance 40 of a .Net 2.0 UI application.
First of all 39 I interviewed users and worked through the 38 use cases myself to come up with a list 37 of target use cases that highlighted the 36 systems worse performing areas. I.e. I 35 didn't want to spend n man days optimising 34 a feature that was hardly ever used but 33 very slow. I would want to spend time, however, optimising 32 a feature that was a little bit sluggish 31 but invoked a 1000 times a day, etc.
Once 30 the candidate use cases were identified 29 I instrumented my code with my own light 28 weight logging class (I used some high performance 27 timers and a custom logging solution because 26 a needed sub-millisecond accuracy). You 25 might, however, be able to get away with 24 log4net and time stamps. The reason I instrumented 23 code is that it is sometimes easier to read 22 your own logs rather than the profiler's 21 output. I needed both for a variety of 20 reasons (e.g. measuring .Net user control 19 layouts is not always straightforward using 18 the profiler).
I then ran my instrumented 17 code with the ANTS profiler and profiled 16 the use case. By combining the ANTS profile 15 and my own log files I was very quickly 14 able to discover problems with our application.
We 13 also profiled the server as well as the 12 UI and were able to work out breakdowns 11 for time spent in the UI, time spent on 10 the wire, time spent on the server etc.
Also 9 worth noting is that 1 run isn't enough, and 8 the 1st run is usually worth throwing away. Let 7 me explain: PC load, network traffic, JIT 6 compilation status etc can all affect the 5 time a particular operation will take. A 4 simple strategy is to measure an operation 3 n times (say 5), throw away the slowest 2 and fastest run, the analyse the remianing 1 profiles.
Eqatec profiler is a cute small profiler that is free and 4 easy to use. It probably won't come anywhere 3 near the "wow" factor of Ants 2 profiler in terms of features but it still 1 is very cool IMO and worth a look.
i just set breakpoints, visual will tell 2 you how many ms between breakpoint has passed. so 1 you can find it manually.
If you don't want to pay, the newer VS verions 16 come with a profiler, but to be honest it 15 doesn't seem very good. ATI/AMD make a free 14 profiler... but its not very user friendly 13 (to me, I couldn't get any useful info out 12 of it).
The advice I would give is to time 11 function calls yourself with code. If they 10 are fast and you do not have a high-precision 9 timer or the calls vary in slowness for 8 a number of reasons (e.g. every x calls 7 building some kind of cache), try running 6 each one x10000 times or something, then 5 dividing the result accordingly. This may 4 not be perfect for some sections of code, but 3 if you are unable to find a good, free, 3rd 2 party solution, its pretty much what's left 1 unless you want to pay.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.