c# - Why this performance difference? (Exception catching) -


While reading a question about what our computer can do in a second, I did a little test, While and I am very surprised with the results. Look:

A simple program for catching zero exception takes approximately one second for 1900 iterations:

 for  (long c = 0; c & Lt; 200000000; c ++) {Try {test = null; Test.x = 1; } Catch (Exception pre) {}}  

Alternatively, if test == faucet is being checked before assigning, then aprox 200000000 repetitions in the same pogram one second Can do.

for (long = 0; c & lt; 1900; c ++) {test = null; F (! (Test == faucet)) {test.x = 1; }}

Why is the description of this vast collection to any person?

Edit: Run the test in the release mode, I'm getting 35,000-40000 iterations versus 400,000,000 iterations (always aprox) outside of Visual Studio

< Strong> Note I have a crappy PIV 3.06Ghz

P>

There is no such way , Unless you are not running in the debugger, then another should take 1900 iterations. Running a demonstration test under the debugger is a bad idea.

Edit: Note that this release is not a case of switching to build - this is a case of running without a debugger; That is, killing Ctrl-F5 rather than F5 .

After saying this, unholy exceptions when you can easily escape from them also is a bad idea.

Take me on the performance of exceptions: If you are using them properly, they should not cause significant performance problems unless you are in some terrible condition (For example, you are trying to call hundreds of web services and the network is down).

exceptions are expensive under debugger - of course, in Visual Studio, anyway - whether or not to break into debugger etc., due to this work, and possibly any amount of stack analysis which is otherwise unnecessary . They are still expensive to to some extent , but you should not throw them enough to notice. Still it is indispensable to do so, to find related cat handles etc. - but it should only happen when something is wrong.

Edit: Make sure you have to give less than throwing exceptions per second (although 35000 is still a very small number - I look forward to more than 100K) because you have almost

  • Assign the variable for zero.

  • Check that the variable is null; It is, therefore, return to the top of the loop

(As described in the comments, it is quite possible that GIT will be eliminated this way ...)

<

Exception version:

  • Assign redundant to the variable
  • Décor variable
    • Full check for empty < / Li>
    • Create an exception object
    • Check for any filtered exception handles to call
    • to jump to catch blocks
    • Also go to the end Yellow block
    • appropriate branch

Is it no wonder that you are seeing less performance?

Now compare that with a more general condition where you do a whole bunch of work, possibly an IO, object creation etc. - and probably an exception is thrown. Then the difference becomes very less important.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -