# C# Reflection Performance And Ruby

DevFeed: [C# Reflection Performance And Ruby](<https://devfeed.tech/articles/c-reflection-performance-and-ruby-33389.md>)

Original publisher: [Read original article](<https://timkellogg.me/blog/2012/02/10/c-reflection-performance-and-ruby>)

Published: 2012-02-10T00:00:00Z

Content type: article

Language: en

Sources: [Tim Kellogg](<https://devfeed.tech/sources/tim-kellogg.md>)

Topics: [C#](<https://devfeed.tech/topics/csharp.md>), [Code](<https://devfeed.tech/topics/code.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [MongoDB](<https://devfeed.tech/topics/mongodb.md>)

Tags: [c-sharp](<https://devfeed.tech/tags/c-sharp.md>), [code](<https://devfeed.tech/tags/code.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [performance](<https://devfeed.tech/tags/performance.md>), [ruby](<https://devfeed.tech/tags/ruby.md>)

## AI overview

The article reports an experiment comparing C# reflection, direct property invocation, closures, and Ruby method invocation. It finds that reflection invocation is roughly a hundred times slower than a normal property setter, while noting that reflection remains useful when a type is not known at compile time.

## Source excerpt

I've always known that reflection method invocations C# are slower than regular invocations, but I've never never known to what extent. So I set out to make an experiment to demonstrate the performance of several ways to invoke a method. Frameworks like NHibernate or the mongoDB driver are known to serialize and deserialize objects. In order to do either of these activities they have to scan the properties of an object and dynamically invoke them to get or set the values. Normally this is done via reflection. However, I want to know if the possibility of memoizing a method call as an expression tree or delegate could offer significant performance benefits. On the side, I also want to see how C# reflection compares to Ruby method invocations.I posted the full source to a public github repo. To quickly summarize, I wrote code that sets a property on an object 100 million times in a loop. Any setup (like finding a PropertyInfo or MethodInfo) is not included in the timings. I also checked the generated IL to make sure the compiler wasn't optimizing the loops. Please browse the code there if you need the gritty details.Before I get into the implementation details, here are the results:You can see that a reflection invoke is on the order of a hundred times slower than a normal property (set) invocation.Here's the same chart but without the reflection invocation. It does a better job of showing the scale between the other tests.Obviously, the lesson here is to directly invoke methods and properties when possible. However, there are times when you don't know what a type looks like at compile time. Again, object serialization/deserialization would be one of those use cases.Here's an explanation of each of the tests:Reflection Invoke (link)This is essentially methodInfo.Invoke(obj, new[]{ value } on the setter method of the property. It is by far the slowest approach to the problem. It's also the most common way to solve the problem of insufficient pre-compile time knowledge.