Introduction
Sometimes as developers we get too focused on creating aesthetically pleasing code and we forget about the real life
implications of our design - are we picking the right class for the job? Or are we using it simply because it's more
convenient to interact with? The latter would be a fair opinion if the costs associated with its use were identical to the
other solutions. Unfortunately in most cases this is not true.
The .NET platform has a wide range of solutions that are generalized. Interacting with the Base Class Library (BCL) and
other associated classes is a part of life as a .NET developer just like using the standard library and Win32 is if you are a
Windows C++ developer. But what distinguishes many choices at the implementation level is speed (after clarity of system
design and composition), and a lot of the .NET stuff can be very expensive to use.
By using a profiler we can analyze our code to see where the bulk of time is being spent in our application and see if we
can find a more appropriate solution to reduce that time. I like referring to time allocation for routines as a
budget. A well planned project will have certain quality standards: Maybe your application has 10 milliseconds to
perform operation X - then 10 milliseconds is your budget for the entire operation. Therefore, you need to make sure that you
meet that quota. Profiling plays a major role by highlighting slow code.
In this review we will talk about using ANTS Profiler 3 to highlight problematic areas of code.
What ANTS Profiler 3 supports
- ANTS Profiler 3 supports two types of profiling:
- Performance profiling (two types: detailed and fast)
- Memory profiling
- Each of which supports the following application types:
- .NET desktop application
- ASP.NET application hosted in IIS
- .NET Service
- COM+ server application
- The support offered by ANTS Profiler is pretty comprehensive. In this review I will be profiling a .NET desktop
application.
The workflow of a profiling session
Profiling an application with ANTS Profiler couldn't be any simpler. You simply start ANTS Profiler and then follow the
wizard, selecting the profiling type and specifying the location of the binary you wish to profile.
Figure 1: Selecting the application type

After
completing the wizard you get a nice overview report that highlights:
- Slowest lines of code
- Slowest methods
- Slowest methods with children
Figure 2: The slow stuff

I would suggest that even
though the profiler may mark a line of code or method as being slow, before you think that your code is not very good think
about the scenario it is satisfying. As a rule of thumb, you should investigate that code to see what is costing you.
As well as statistical data in the form of a summarized report, you also get a preview of your source code along with cost
annotations that make it easier to identify the more expensive code in your application.
Figure 3: Code cost annotations

In
Figure 3 we have hit counts displayed. If a line of code or method is pretty expensive and the hit count is high then you are
probably going back to the whiteboard. Like I mentioned earlier, everything is within context. The profiler may identify a
line of code as being really slow yet you know that you maybe have to touch the disk for a certain operation and that is the
bulk of the cost there.
As well as the summary and code annotations you also have an All Methods view which allows you to see every method called
and its associated call time. You will probably spend the majority of time in this screen, filtering by time and hit count
to check that the code under stress is as efficient as possible.
Figure 4: All methods screen

Note: I've squeezed the columns together for the screenshot. All columns can be customized and filtered.
Memory Profiling
This is another great way to look at what resources you're program is consuming, in particular the size of the managed
heap.
The way the memory profiler works is driven by the notion of a snapshot. You take as many snapshots as you want to
see what the allocation looks like at the various stages of your applications lifespan.
The memory profiler has a Summary screen, All objects screen and All classes screen.
Figure 5: All classes screen

The memory
profiler is useful as it highlights expensive types that will consume a fair amount of memory, although bytes are fairly
trivial in small applications. If you have a type that will spawn 10 or so thousand objects then it's a different story.
Exporting results
There are two options when exporting the profiling data from a particular session: These are XML or HTML. The HTML
version is simply a table that replicates the All Methods view found in the profiler application itself.
Figure 6: Results exported as HTML

Summary
This is an awesome tool, and ever since downloading it I have been using it on a daily basis and just love the detail of
the reports. The snapshot feature for memory profiling can be a little strange at first but after playing with it for a
while you will absolutely love it. The whole experience couldn't be better.
ANTS Profiler 3 is one of my favorite all time tools - definitely recommended if your job entails producing highly
optimized code - this is a must have!
About Granville Barnett
 |
Sorry, no bio is available
This author has published 32 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
November's Toolbox Column Now Online
read more
October's Toolbox Column Now Online
read more
September's Toolbox Column Now Online
read more
August's Toolbox Column Now Online
read more
July's Toolbox Column Now Online
read more
Load Testing vs. Profiling
read more
An Overview of Wrox Series
read more
AjaxView Javascript profiling
read more
Due Diligence and Code Comments
read more
The Joy of Code Reviews
read more
|
|
Please login to rate or to leave a comment.