Identifying bottlenecks
There are a number of techniques to identify pieces of code with high levels of CPU usage:
Focus on pieces of code that are executed frequently. Loops, and especially loops within loops, are good candidates. If you sort collections using a class derived from IComparable, the code in that class will be very busy. If you retrieve data from a database, each record needs to be processed in turn, using some sort of loop.
You could instrument your code with lightweight counters, to measure how often each section is executed and how long it takes to execute. Counters are discussed in the Measuring wait times section in Chapter 7,
Do some old-style debugging. Run a stress test on your development site, so that it uses a lot of CPU. Take out one half of the code, and see what difference this makes to CPU usage. If CPU usage goes down considerably, the culprit is probably in the half you took out. Keep hacking until you isolate the offending code.
If you do non-trivial things...