Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profiling .NET applications with Stopwatch

It seems there are no free* .NET performance profilers that can profile on a line-by-line basis. Therefore, I am looking into using Stopwatch for profiling.

*free as in freedom, i.e. license includes commercial applications.

EDIT: In response to those who told me "buy a profiler", I would like to, but if I could spend that much money I would spend it on something else. I tried to convince my boss that a profiler is worth it, but haven't had much luck. This question is mostly based on curiosity. I would never consider Stopwatch as a replacement for a real profiler.

I have a little test app (written in C#) that measures performance differences when using a Stopwatch on a per-line basis. The test code is this:

int n = 100;
BigInteger f = 1;
for (int i = n; i > 1; i--)
{
    f *= i;
}

Here is the full code: http://pastebin.com/AvbQmT32

I have a Stopwatch for each line of code. This is my 'profiler'. I also have one Stopwatch for the whole program. This is my 'profiler profiler'.

I have the program configured as Release mode, Any CPU (on an x64 machine), and optimizations disabled.

When I run the program with the profiler disabled, I get something like this:

             Line             |  Ticks
------------------------------|----------
                              |
Total time:                   |       359

When I run it with the profiler enabled, I get something like this:

             Line             |  Ticks
------------------------------|----------
                              |
int n = 100;                  |         3
BigInteger f = 1;             |        12
for (int i = n; i > 1; i--)   |       325
{                             |
    f *= i;                   |       539
}                             |
                              |
Total time:                   |      1710
Stopwatch overhead:           |       831

Ideally, the time spent on code should be equal in both cases, but it appears that the Stopwatches have overhead that appears within their own elapsed time.

Now, it doesn't often make sense to need to profile every line of a program, as it usually works better with a divide-and-conquer approach. You can usually start by profiling chunks of code, and narrow down on any performance problems.

Also, in most applications, the average line of code will be a lot slower than the ones in the test program. This means that there will be less Stopwatch overhead.

However, there is still overhead when using Stopwatches, especially if you use a lot.

So down to the question:

What is the most effective way to use Stopwatches for profiling? How can I minimize overhead? Is it even worth it to wrap a Stopwatch around a single statement?

I appreciate your feedback.

like image 449
Kendall Frey Avatar asked Mar 22 '12 15:03

Kendall Frey


People also ask

What is stopwatch used for in C#?

Stopwatch is a class in C# to measure the elapsed time. Use it to calculate the time a function took to execute. It is found under System. Diagnostics.

Is stopwatch thread-safe C#?

Looking at the source code, it is not thread-safe.

How accurate is the stopwatch class C#?

Stopwatch class does accurately measure time elapsed, but the way that the ElapsedTicks method works has led some people to the conclusion that it is not accurate, when they really just have a logic error in their code.

What is profiler C#?

A profiler is a tool that monitors the execution of another application. A common language runtime (CLR) profiler is a dynamic link library (DLL) that consists of functions that receive messages from, and send messages to, the CLR by using the profiling API. The profiler DLL is loaded by the CLR at run time.


1 Answers

Considering that we often intuitively program by Single responsibility principle, not only in regard of types but the functions too, I would say there is no any sence of profiling application line by line. You will be more interested in profiling a "single responsibility" then every single line.

There are, naturally, cases when you need to have informaiton too. But do not use it in all application but in single part of a function or a single function. In this case StopWatch is a best choice. Consider that StopWatch is .NET Class so has it's even minimum overhead. You shouldn't look on absolute, but on relative values.

Hope this helps.

like image 99
Tigran Avatar answered Sep 16 '22 15:09

Tigran