Coffeehouse Thread

28 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Try catch and not affecting speed

Back to Forum: Coffeehouse
  • User profile image
    littleguru

    undefined Big Smile

  • User profile image
    Sven Groot

    Is this in .Net? C++ try/catch definitely has overhead. I think I read in .Net it should not make a difference, unless an exception is actually thrown.

    It's hard to judge what's going on in your benchmark without seeing the code and knowing the method of measurement.

  • User profile image
    littleguru

    for (int g = 0; g < 10; g++)

    {

    for (int j = 0; j < 10000000; )

    {

    try

    {

    int i = j;

    i++;

    j = i;

    }

    catch (Exception e)

    {

    throw;

    }

    }

    double d = ((TimeSpan)(DateTime.Now - now)).TotalMilliseconds;

    Console.WriteLine("Try " + d);

    now = DateTime.Now;

    for (int j = 0; j < 10000000; )

    {

    int i = j;

    i++;

    j = i;

    }

    double d2 = ((TimeSpan)(DateTime.Now - now)).TotalMilliseconds;

    Console.WriteLine("No Try " + d2);

    }

  • User profile image
    littleguru

    It is C#. And I know it's much calling the thing 10 million times each, but i had to test it, since the thing must also be able to run under heavy load.

  • User profile image
    MasterPi

    Wouldn't the try catches require additional CPU time to figure out the exception, etc. I heard it's better to use an If..Then.. if you are certain an error is possible.

  • User profile image
    Sven Groot

    You should never use try/catch for ordinary flow control. Throwing an exception is incredibly expensive. But just putting a try/catch block around an operation should not make that much difference.

  • User profile image
    Sven Groot

    littleguru wrote:
    Forget it... had some issues here... i'm a jerk!

    What was the problem?

  • User profile image
    littleguru

    Forget it... had some issues here... i'm a jerk!


    It is EQUAL without and with try block!

  • User profile image
    littleguru

    Sven Groot wrote:
    littleguru wrote: Forget it... had some issues here... i'm a jerk!

    What was the problem?


    Look where I'm setting now in my code ... Before the "non try" run and nowhere else... so the second time of the try run is always the accumulated time of both runs... shame on me!

  • User profile image
    Sven Groot

    Ah I see. Shame on you! Wink

  • User profile image
    littleguru

    Correct times:

    Try 230,3312
    No Try 300,432
    Try 240,3456
    No Try 270,3888
    Try 250,36
    No Try 260,3744
    Try 240,3456
    No Try 290,4176
    Try 230,3312
    No Try 290,4176
    Try 240,3456
    No Try 250,36
    Try 240,3456
    No Try 270,3888
    Try 230,3312
    No Try 280,4032
    Try 230,3312
    No Try 290,4176
    Try 240,3456
    No Try 270,3888

    Now the times seem to be equal.

    The code looks now this way:

    for (int g = 0; g < 10; g++)

    {

    DateTime now = DateTime.Now;

    for (int j = 0; j < 10000000; )

    {

    try

    {

    int i = j;

    i++;

    j = i;

    }

    catch (Exception e)

    {

    throw;

    }

    }

    DateTime then = DateTime.Now;

    double d = ((TimeSpan)(then - now)).TotalMilliseconds;

    Console.WriteLine("Try " + d);

    now = DateTime.Now;

    for (int j = 0; j < 10000000; )

    {

    int i = j;

    i++;

    j = i;

    }

    then = DateTime.Now;

    d = ((TimeSpan)(then - now)).TotalMilliseconds;

    Console.WriteLine("No Try " + d);

    }


    I should think first then post.

  • User profile image
    littleguru

    Sven Groot wrote:
    Ah I see. Shame on you!


    But I can do my tracing now... fine! Big Smile

  • User profile image
    Sven Groot

    If you want to do real tracing, I suggest using DevPartner Profiler Community Edition. It's a real nifty free profiler that integrates with Visual Studio and give you line-by-line statistics on what time everything takes.

  • User profile image
    littleguru

    I'm adding tracing info to my program by using Log4NET. Okay, I made it that way that you can use every framework you want, by implementing an interface...

  • User profile image
    rhm

    Actually throwing (and subsequently catching) an exception takes approximately forever. But this question is about the overhead of putting in the try/catch box when an exception isn't thrown am I right?

    I don't think the overhead is massive, but there must be some overhead because the stack frame has to be set up at some point so that should an exception be thrown, the throwing routine knows where to stop. That pushing and popping of stuff from the stack around the try block takes time, but not enough to worry about.

  • User profile image
    littleguru

    There is no overhead. It was a mistake of me.

  • User profile image
    figuerres

    littleguru wrote:

    for (int g = 0; g < 10; g++)

    {

    for (int j = 0; j < 10000000; )

    {

    try

    {

    int i = j;

    i++;

    j = i;

    }

    catch (Exception e)

    {

    throw;

    }

    }

    double d = ((TimeSpan)(DateTime.Now - now)).TotalMilliseconds;

    Console.WriteLine("Try " + d);

    now = DateTime.Now;

    for (int j = 0; j < 10000000; )

    {

    int i = j;

    i++;

    j = i;

    }

    double d2 = ((TimeSpan)(DateTime.Now - now)).TotalMilliseconds;

    Console.WriteLine("No Try " + d2);

    }



    FWIW and IMHO  I'll just say that Try / catch are often abused and mis-used

    the idea is to hand "Exceptional Conditions"  not problems you can expect and handle with a bit of good code.

    for example you have a loop that has a try/catch block inside it.

    while I know you are in this case trying to find the amount of overhaead it has the example is not going to really reflect any "real world" program.

    If I had a coder who ever put a try/catch in a loop I'd have him explaining to me why he did it.

    most of my code has *NO* block but my app does have a try catch block that gets called by a global handler to do an "Sorry we have a bad problem and need to report the bug..." thing.

    if you know that a file open may not work then code to deal with that, for example if it's a file you want to read ask if it is there and then ask of you have read access then open it.

    harder to code that? not by much....

    if you go the try catch road you need to check the error in the catch and then figure out what to do.

    if you go the way I just outlined you have a set clear points where you can handle the problem and never throw an excpetion.

  • User profile image
    Minh

    Sven Groot wrote:
    C++ try/catch definitely has overhead.
    Really? I thought that C++, like C#, has no exception performance penalties, unless thrown. I'm a C++ newbie, so can you elaborate?

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.