Posts

Showing posts from September, 2007

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

Know thy exceptions

Image
A nother performance query: What is the cost of an arbitrary try-catch? To your right, you should see the results. Note that it took 2.5 times longer to execute the same code when it was inside a Try-Catch-block even though No exception was thrown . Don't get me wrong - the cost is often worth it. However, given the cost, it is wise to study exactly what Exceptions can be thrown in what scenarios, rather than just sprinkle your code with Try-Catches everywhere. The code that was executed, was: Private Function WithTryCatch () As Integer    Dim number As Integer    For index As Integer = 0 To CInt ( Integer . MaxValue / 2 )      Try        number += 1      Catch ex As Exception      End Try    Next    Return number End Function Private Function WithoutTryCatch () As Integer    Dim number As Integer    For index As Integer = 0 To CInt ( Integer . MaxValue / 2 )      number += 1    Next    Return number End Function