C# - Exception Performance

added by mistralol
9/7/2011 9:25:17 AM

193 Views

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.


6 comments

dpeterson
9/7/2011 9:25:21 AM
An interesting article, though I'm not sure we should really be concerned with the performance of our exceptions. They really shouldn't be used for program flow control or anything like that, so I think even a second of wait time is acceptable for handling an exception as exceptions are exceptional after all ;)

mistralol
9/7/2011 12:17:07 PM
They do start to make a major difference if you are importing a number of rows into a database eg 1 million+ and you have errors on even 0.01% of them :)

dpeterson
9/8/2011 8:09:43 AM
I generally treat imports as atomic operations, but if you're not doing that (where the operation doesn't need to succeed or fail as one) I can see where the exceptions could slow things down.

If you explicitly catch database errors at the lowest level, is the effect lessened?

vijayst
9/8/2011 10:31:23 AM
A very interesting analysis. I am curious to know the iteration that raised the slowest exception. Is it the last exception by any chance?

For the second performance test, I am curious to know the elapsed time if the loop decremented from 20 to 1 (instead of 1 to 20).

mistralol
9/8/2011 12:23:27 PM
They appeared to be random. The actual performance problem only occurs inside the debugger. So its task switching to visual studio which probably accounts for being completely random.

vijayst
9/9/2011 11:11:07 PM
Thanks for the reply. I will do some more analysis with your code, when I have the time.