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.



Recent Comments