Know thy exceptions, part 2

(At the time of writing this, I have three comments on the previous article. That is the background to this post)

With a modified version of the below application, where I wrap the entire loop into a try-catch-statement instead of every iteration, and multiplying the number of iterations by 100 (for better statistical value), a debug-release generates the following:

VB.NET - Debug Build

  • With Try-Catch (100 iterations): 356433 ms
  • Without Try-Catch (100 iterations): 175644 ms
  • Diff: 180790 ms (49%)
VB.NET - Debug Build - Single Iteration, 3rd execution
  • With Try-Catch (100 iterations): 4703 ms
  • Without Try-Catch (100 iterations): 1750 ms
  • Diff: 2953 ms (37%)
VB.NET - Release Build
  • With Try-Catch (100 iterations): 248033 ms
  • Without Try-Catch (100 iterations): 175517 ms
  • Diff: 72516 ms (71%)
VB.NET - Release Build - Single Iteration, 3rd execution
  • With Try-Catch (100 iterations): 2516 ms
  • Without Try-Catch (100 iterations): 1750 ms
  • Diff: 766 ms (70%)

C# - Debug Build
- coming soon -

C# - Release Build
- coming soon -

Comments

Anders said…
I don't get quite the same numbers,
but I agree it looks like having a
Try-Catch does come with a performance-penalty.

I still think though, that if your functions are doing anything else that pure mathematics and/or variable assignment (such as DB-reads, file-manipulation, string-parsing or whatnot), the try-catch penalty is becoming a very very small part of the actual execution time of the function.

In practice it will become a negligible parameter in system development unless you are doing very computational-heavy work and/or for example real-time systems with extreme demands on execution/response time(in which case you probably won't use VB .NET on a windows platform anyhow).

As a side note, I tried making the iteration as an infinite loop that breaks when the iterator reaches a certain value, and that proved to be faster (66% exec time) than using the for loop without any error handling, thus being the fastest method of all AND fail-safe since I use an IF to check the only value that could possibly go wrong.

Can we draw any conclusions of this? I would think not, since the
code is still not representative of any real-life application that actually has a purpose.

Also, try-catch has the added benefit of catching the exceptions that we as programmers do NOT expect. Now, I know most of you probably make bug-free code from day one, but I'll still rely on my try-catch, thank you very much :D

/Anders (forever pragamatic and evil)

Popular posts from this blog

Auto Mapper and Record Types - will they blend?

Unit testing your Azure functions - part 2: Queues and Blobs

Testing WCF services with user credentials and binary endpoints