Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Reflection Performance

Tags:

c#

reflection

I am playing about the static reflection code from Joel Abrahamsson's blog and Daniel Cazzulino's blog. But I found their performance is kind of slow, even comparing with refection using "magic string".

int iterations = 1000000; 
watch.Start();

for (int i = 0; i < iterations; i++)
{
    var propertyOfName = Reflect<Employee>.GetProperty(c => c.Name); 
}

watch.Stop();
Console.WriteLine("[Reflector]: " + watch.ElapsedMilliseconds.ToString());
watch.Reset();
watch.Start();

for (int i = 0; i < iterations; i++)
{
    var propertyName = typeof (Employee).GetProperty("Name"); 
}

watch.Stop();
Console.WriteLine("[Regular Reflection]: " + watch.ElapsedMilliseconds.ToString());
watch.Reset();
watch.Start();

for (int i = 0; i < iterations; i++)
{
    var propertyName = StaticReflection.GetMemberName<Employee>(c => c.Name);
}

watch.Stop();
Console.WriteLine("[StaticReflection]: " + watch.ElapsedMilliseconds.ToString());

Here is the result:

  • [Reflector] : 37823
  • [Regular Reflection]: 780
  • [Static Reflection]: 24362

So why should we prefer Static Reflection? Just remove "magic string"? Or we should add some caching to improve static reflection performance?

like image 810
Liang Wu Avatar asked Aug 15 '11 02:08

Liang Wu


People also ask

Is reflection bad for performance Java?

Reflection is 104% slower than direct access (so about twice as slow). It also takes longer to warm up.

Why is Java reflection so slow?

Reflection is slow for a few obvious reasons: The compiler can do no optimization whatsoever as it can have no real idea about what you are doing. This probably goes for the JIT as well. Everything being invoked/created has to be discovered (i.e. classes looked up by name, methods looked at for matches etc)

Is C# reflection slow?

Reflection is not THAT slow. Invoking a method by reflection is about 3 times slower than the normal way. That is no problem if you do this just once or in non-critical situations. If you use it 10'000 times in a time-critical method, I would consider to change the implementation.

Is it good practice to use reflection?

It is generally a bad idea to use reflection is application code, because you lose the strict type checking of the language. Reflection is generally for use by framework code, where it is essential. Good vs bad are not absolutes, but must be assessed in context.


1 Answers

The main reason to "prefer" it would be static type checking by the compiler, to ensure you don't make a mess of it (and to ensure it works if you obfuscate, for example). However, IMO it is so rare that a typo here is a significant bug (meaning: I'm not including the brain-dead typo you spot and fix during development / unit-testing); so (since I am a performance nut) I typically advise to use the simplest option (string). A particular example of this is when people are implementing the INotifyPropertyChanged interface using tricks like this. Just pass a string ;p

like image 134
Marc Gravell Avatar answered Sep 19 '22 01:09

Marc Gravell