views:

54

answers:

3

Is the execution time (run-time) performance of code in a class that is loaded via reflection identical to the same code when the class is created using the new keyword?

I say yes. But I was discussing this with a colleague who believes that the reflection oriented code is always slower.

My view is that regardless of how the class was originally loaded/created, the performance will be identical because the JIT compiler does not care how a class was loaded.

Am I correct? Either way, I'd appreciate any references that can help clarify this.

(NB: I'm not talking about the performance of creating a class using reflection versus the new keyword. I'm referring to the actual code in methods of the class after it has been created.)

+1  A: 

Yes, once loaded the performance is the same.

The performance penalty of reflection is bound to the reading of the metadata from the assembly but the execution time will be exactly the same. That is, once the instance has been created and you have a reference to it, it will behave as any other class you have (including JIT compiling and everything).

Jorge Córdoba
Any references such as web pages, blogs etc?
Ash
Not really, just Chapter 4 of .NET Framework from Joe Duffy. If you have access open up that chapter and read up Assembly loading part and Inside Assembly metadata part.
Jorge Córdoba
A: 

It depends on how you use reflection. It's always slower, but you can make the time difference really small if you use IL emit to create a factory method in runtime. If you use the simple Activator.CreateInstance, it will be so much slower.

Diego Jancic
You misunderstand my question, see the NB.
Ash
Yes, you're right. Sorry. Jorge is right, the code execution is the same one the code was called.
Diego Jancic
+1  A: 

It depends on how you execute it ;-p

Once you're inside the methods on the loaded type, yes: regular GIT etc applies normally (note that security checks may make things a little slower if it is partially trusted, but not much).

But first you need to invoke some code on the dynamic object:

  • If you can cast the object to an interface or base-class that is known statically, then it will be identical.
  • If this isn't possible, but you can bind specific operations to known delegates (for example Func<string,int>, via Delegate.CreateDelegate), then it will be almost as fast, but less convenient.
  • If you do everything via DynamicInvoke(), it will be pretty treacle-like.
  • In 4.0, dynamic may offer a halfway house, in that it offers duck-typing with optimised caching per type.

So: how are you accessing it?

Marc Gravell
Marc, We use Activator.CreateInstance() and cast the returned object to a known class type. From then on it's always accessed through this strongly typed instance. We're working with c#2 and moving to C#3/4 hopefully soon.
Ash
Interesting point about security checks. That may be what my colleague was referring to, although they didn't say so at the time.
Ash
I think your colleague is being misled to the difference between *loading* something via reflection, and *calling everything* via reflection. It sounds like it should work fine in your scenario with a known class type.
Marc Gravell