C# - Exception Performance

6. September 2011 20:58

 

In any language exceptions are great. They make the problem of handling error's a lot easyier since you don't need to check the values that are being returned from functions which saves in writting lots of code. It also make the code structure better as it clumps the error handling code into a single place which can deal with multiple errors when used correctly.

 

However in c# I have a noticed a few problems with performance of even the most basic code when dealing with exceptions. The first is that they can actually take a very long time to run. They can also take a rather unpredictable time to run even when used in the most simple situations. Let have a look at the following example in c# of a basic exception handler benchmark test.

 

You should also be aware that this only happens under visual studio when a debugger is attached to the application.

 

static void Main(string[] args)
{
	long Fastest = long.MaxValue;
	long Slowest = long.MinValue;


	for (int i = 0; i < 500; i++)
	{
		Stopwatch W = new Stopwatch();
		W.Start();

		try
		{
			throw (new Exception());
		}
		catch (Exception)
		{

		}

		W.Stop();
		if (W.ElapsedMilliseconds < Fastest)
			Fastest = W.ElapsedMilliseconds;

		if (W.ElapsedMilliseconds > Slowest)
			Slowest = W.ElapsedMilliseconds;

	}

	Console.WriteLine("Fastest Exception: {0}", Fastest);
	Console.WriteLine("Slowest Exception: {0}", Slowest);
}

 

As you can see the benchmark code gets the fastest and slowest time it take for an exception to run. The results when I ran it were somewhat shocking.

 

Fastest Exception: 11
Slowest Exception: 468

 

The fact that it can take almost half a second to handle an exception is somewhat crazy. I tested for the above because I was having some performance issues when using exceptions recursivly or when using deeply nested code which is very common in larger or more complex systems. So I put the following simple code together to test the performance for them.

 

static void Main(string[] args)
{

	for (int i = 0; i < 20; i++)
	{
		Stopwatch W = new Stopwatch();
		W.Start();
		try
		{
			Run(i);
		}
		catch (Exception )
		{

		}
		W.Stop();
		Console.WriteLine("Depth: {0} Time: {1} ms", i, W.ElapsedMilliseconds);
	}

	Console.ReadLine();
}

static void Run(int Depth)
{
	try
	{
		if (Depth > 0)
			Run(Depth - 1);
		else
			throw (new Exception());
	}
	catch (Exception ex)
	{
		throw (ex);
	}
}

 

Again the results are somewhat shocking.

 

 

Depth: 0 Time: 25 ms
Depth: 1 Time: 40 ms
Depth: 2 Time: 53 ms
Depth: 3 Time: 70 ms
Depth: 4 Time: 86 ms
Depth: 5 Time: 101 ms
Depth: 6 Time: 118 ms
Depth: 7 Time: 137 ms
Depth: 8 Time: 157 ms
Depth: 9 Time: 178 ms
Depth: 10 Time: 201 ms
Depth: 11 Time: 222 ms
Depth: 12 Time: 245 ms
Depth: 13 Time: 268 ms
Depth: 14 Time: 291 ms
Depth: 15 Time: 317 ms
Depth: 16 Time: 343 ms
Depth: 17 Time: 370 ms
Depth: 18 Time: 398 ms
Depth: 19 Time: 421 ms

 

The above results show that after only 5 layers deep in code that it can be taking 1/10th of a second to handle an error. It also appears that for every set of try / catch / throw code block it will add around 20-30ms of delay in unwinding the stack to handle the exception.

E-mail Kick it! DZone it! del.icio.us Permalink


Comments (3) -

9/7/2011 7:30:23 PM #

Hi, what is your setup? I ran both your code samples and they both return 0ms without a debugger attached.

high United States |

9/7/2011 7:37:57 PM #


Yes it was in a debugger. I didn't think that visual studio would cause the performance issue.

james United Kingdom |

9/7/2011 7:42:04 PM #

The debugger makes a huge difference. Whenever you are testing the performance of something you should really test it without the debugger attached.

www.codermanager.com/.../

So what you are seeing here is simply the debugger being slow. This has nothing to do with exceptions being slow.

high United States |