views:

607

answers:

6

I have an event handler that needs to determine a type and execute code if it matches a specific type. Originally we cast it to an object and if it wasn't null we executed the code, to speed it up I used reflection and it actually slowed it down and I don't understand why.

here is a code sample

Trace.Write("Starting using Reflection");
if (e.Item.GetType() == typeof(GridDataItem))
{
      bool isWatch = Convert.ToBoolean(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IsWatch"]);
      if (isWatch)
      {
          e.Item.Style["Font-Weight"] = "bold";
      }
 }
 Trace.Write("Ending using Reflection");
 Trace.Write("Starting using Cast");
 GridDataItem gridItem = e.Item as GridDataItem;
 if (gridItem !=null)
 {
     bool isWatch = Convert.ToBoolean(gridItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IsWatch"]);
     if (isWatch)
     {
         gridItem.Style["Font-Weight"] = "bold";
     }
  }
  Trace.Write("Ending using Cast");

And this is the trace output I get

Starting using Reflection  0.79137944962406 0.576538
Ending using Reflection    0.791600842105263    0.000221
Starting using Cast    0.791623353383459 0.000023
Ending using Cast      0.791649308270677 0.000026
Starting using Reflection  0.876253801503759    0.084604
Ending using Reflection    0.87631790075188 0.000064
Starting using Cast    0.87633445112782 0.000017
Ending using Cast      0.87634950075188 0.000015

it's not a lot, but if we had to do this a lot over time it could add up.

+11  A: 

Reflection is slow because you are querying the assembly's metadata whereas casting simply changes the type of the object you are referencing.

The assembly's metadata is a useful store of information but that information is best used at compilation time rather than at execution time. That metadata is used by the compiler for static type checking (among other things). You are using that same metadata to look up type information at execution time (which is fine if you have no other choice) which is significantly slower than casting.

Andrew Hare
+2  A: 

Reflection has to go at runtime and determine what properties etc. the object has at runtime. Casting tells the application that it should expect that an object has X properties and should function in a certain way.

Kevin
+2  A: 

Casting tells the runtime you "know" the type of a particular object. While you may possibly be wrong the runtime believes you and doesn't take the extra time needed to go check the assembly's meta data.

William Edmondson
+1  A: 

Why aren't you using the is operator? I think it's more readable as you don't have the explicit cast, then check. It simply checks that the variable is of the correct type.

if (e.Item is GridDataItem)
{
    bool isWatch = Convert.ToBoolean(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IsWatch"]);
    if (isWatch)
    {
        e.Item.Style["Font-Weight"] = "bold";
    }
}
tvanfosson
Actually "is" does do a cast under the covers.
Andrew Hare
My point was that it simplifies the code (making it more readable), not that it does anything fundamentally different. I'll clarify.
tvanfosson
That's Actually Slower Starting 1.5268064962406 1.311905 Ending 1.52701457443609 0.000208 Starting 1.59114193383459 0.064127 Ending 1.59120311278195 0.000061
Bob The Janitor
Isn't it true that "is" and "as" do not use conversion operators whereas casting does? If so, a cast is not really done "under the covers" for "is".
Larry Fix
@Bob that seems odd since according to the C# reference, as is equivalent to: expression is type ? (type)expression : (type)null. See the remarks at http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx.
tvanfosson
...perhaps the first reference requires getting it from memory and the second uses it from the cache. Have you tried them in the opposite order?
tvanfosson
for the speed All I know is that's what the data is saying, and if you compare the times using "is" is closer in time to using reflection
Bob The Janitor
A: 

Well i giess a short answer to the best practice part would be never use reflection if you can get the same result with regular code.

When optimizing code it's usually a good Idea to estimate where the time spent optimizing will result in the greatest performance gain. reimplementing operators natively in the language is seldom going to be on the top of that list

Rune FS
+1  A: 

Casts can be done as integer comparisons within the runtime but reflection involves full method calls.