Exception Handling vs. Reflection calls. Wich is more expensive?

programming Add comments

In terms of performance, it is well known that Reflection Calls and Exception Handling are some of the most expensive operations in a programming language.
However, which of them is more expensive?

It only makes sense to compare the performance of these two mechanisms, if you have a situation on where one can replace the other. I have to admit that if a problem can be solved with Reflection or Exceptions, then the solution for this problem smells very bad.
That is exactly the situation which brought up this question, a stink code. My team is working with the NVelocity library, a version of the popular Java Template Engine, ported to MS .Net. This lib makes extensively use of the Code by Exception anti-pattern and refactoring its code is not an option due to the lack of time.
It is weird, but in that code there is a comment suggesting that a reflexive call could avoid a Exception Raising/Handling.

I don’t know about you, but I was surprised by the result. A reflexive call is 5 times faster than an Exception raising and handling.

The following code (in CSharp) runs a Reflexive call and an Exception raising/handling 10.000 times.


public void LoopExceptionAndReflectionCallsTest()
        {
            const int max = 100000;

            if (!Stopwatch.IsHighResolution)
                Console.WriteLine("This machine has no high resolution timer available. Results may be affected.");

            MeasureException(max);
            MeasureReflection(max);
        }

        private void MeasureReflection(int max)
        {
            Stopwatch sw = Stopwatch.StartNew();
            Foo foo = new Foo();

            for (int i = 0; i < max; i++)
            {
                foo.GetType().InvokeMember("someField", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic, null, foo, null);
            }
            sw.Stop();

            Console.WriteLine("Time Elapsed w/ Reflection: {0} ms", sw.Elapsed.TotalMilliseconds);
        }

        private void MeasureException(int max)
        {
            Foo foo = new Foo();
            Stopwatch sw = Stopwatch.StartNew();

            for (int i = 0; i < max; i++)
            {
                foo.RaiseandCatchException();
            }
            sw.Stop();

            Console.WriteLine("Time Elapsed w/ Exception: {0} ms", sw.Elapsed.TotalMilliseconds);
        }
    }

    sealed class Foo
    {
        private int someField = 1;
        public void RaiseandCatchException()
        {
            try
            {
                throw new Exception("SomeException");
            }
            catch (Exception)
            {
            }
        }

        public int DoNothing()
        {
            return someField;
        }
    }
Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists

2 Responses to “Exception Handling vs. Reflection calls. Wich is more expensive?”

  1. Felipe Says:

    I think that, depending on the reflection operation you’re doing, it may become more expensive. In this case, you used a simple InvokeMember call, but it can get a lot more complex than this..

  2. Franklin Dattein Says:

    Actually, all other reflection calls have almost the same performance. It doesn’t matter on how complex is the call.
    Off course, in performance comparison like this, we have to avoid executing an operation that might interfer in the result, so it has to be as simple as possible.

    You can change the code above and make a test.

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in