Chapter 4. CPU
Unlike bottlenecks such as memory shortage that can be relatively cheaply addressed by adding hardware to the web server, upgrading or adding CPUs is relatively expensive. That makes it attractive to first try to reduce CPU usage when confronted by a CPU-related bottleneck.
In this chapter, we'll discuss the following:
Techniques and tools to identify code that require a lot of CPU usage
Specific ways to reduce CPU usage such as more efficient data access, better use of exceptions, and more efficient data binding
Let's start off with identifying where your code incurs the greatest CPU usage.
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...