Exception Overhead - Part 2

Yesterday I looked at the overhead associated with throwing an exception. Driving into work today, I wondering, "what's the cost involved in creating the exception object?"

So I added this method:

public sealed class Methods
{
    public void DoGood()
    {
    }
    
    public void DoBad()
    {
        throw new NotImplementedException();
    }

    public void DoSomething()
    {
        var exception = new NotImplementedException();
    }
}

And added it to the timings:

private static void TimeExceptions()
{
    const int iterations = 500000;

    var methods = new Methods();
    
    Console.Out.WriteLine("Bad Action: " + new Action(() =>
    {
        for(var i = 0; i < iterations; i++)
        {
            try
            {
                methods.DoBad();
            }
            catch(NotImplementedException)
            {
            }
        }
    }).Time());

    Console.Out.WriteLine("Good Action: " + new Action(() =>
    {
        for(var i = 0; i < iterations; i++)
        {
            methods.DoGood();
        }
    }).Time());

    Console.Out.WriteLine("Some Action: " + new Action(() =>
    {
        for(var i = 0; i < iterations; i++)
        {
            methods.DoSomething();
        }
    }).Time());
}

Here are the results:

Bad Action: 00:00:21.3727088
Good Action: 00:00:00.0005447
Some Action: 00:00:04.9421599

Creating the object itself takes time. But throwing the exception adds a lot on top of that.

Somebody was wondering if I compared the time between two methods where one calls a stored proc that does nothing, and the other calls a stored proc that throws an exception. I may look into that.

* Posted at 04.02.2009 10:26:08 AM (Last Update: 04.11.2009 03:56:50 PM) | 4 comments | Link | RSS *

Comments

# Software Engineer, from Rich at 04.03.2009 12:18:29 PM

What about throwing an exception you already caught, possibly to have higher level code handle the exception? For example:

try
{
//code
}
catch(Exception)
{
throw;
}

# I don't follow, from Jason Bock at 04.03.2009 12:23:03 PM

Rich,

Are you asking about what the performance of that would be?

"catch(Exception)" - yikes! ;)

# DoGood optimized away?, from Sasha Goldshtein at 04.11.2009 02:51:26 PM

Your conclusion is probably right in general, but please note that your DoGood method is likely optimized away because it doesn't do anything, making your loop a nop and your measurement a measurement of nothing in that case.

Consider putting [MethodImpl(MethodImplOptions.NoInlining)] on the DoGood method.

# Good point, from Jason Bock at 04.11.2009 03:56:48 PM

Sasha,

That's a good point, and you're right, it does make a difference, but nothing major. The time for the DoGood() timings goes from 0.0005 to 0.002. Comparing them to each other is a jump, but not when you compare 0.002 to 21 :)

Add a Comment

(*) = Required field
Name (*):

E-Mail (*):

Web Site:

Title (*):

Comments (*):

Enter the code you see (*)



Quote
"The primary task of the software development team is to engineer the illusion of simplicity." Grady Booch
Twitter History
follow me on Twitter
Blog History