What is the performance cost of using dynamic
vs object
in .NET?
Say for example I have a method which accepts a parameter of any type. E.G.
public void Foo(object obj) { }
or
public void Foo(dynamic obj) { }
ILSpy tells me that when using dynamic code, the compiler must insert a code block to handle dynamism. Therefore I want to know if using dynamic in place of object is advised and to what level this usage comes at the cost of performance?
The dynamic type has been added to C# since version 4 as because of the need to improve interoperability with COM (Component Object Model) and other dynamic languages. While that can be achieved with reflection, dynamic provides a natural and more intuitive way to implement the same code.
Dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at runtime.
The object type can be passed as a method argument and method also can return object type. Var type cannot be passed as a method argument and method cannot return object type. Var type work in the scope where it defined. Dynamic type can be passed as a method argument and method also can return dynamic type.
Reflection can invoke both public and private members of an object while dynamic can only invoke public members. dynamic is instance specific: you don't have access to static members; you have to use Reflection in those scenarios.
That would depend a lot on the exact scenario - but there is a layer of caching built in, so it is not as terrible as you might expect (it doesn't do reflection every time). It can also vary on the operations (for example, "lifted" nullable-T operations are noticeably slower). You would need to measure, but as it happens I have some timings here for member (property) access, that I took when doing FastMember:
Static C#: 14ms Dynamic C#: 268ms PropertyInfo: 8879ms (aka reflection) PropertyDescriptor: 12847ms (aka data-binding) TypeAccessor.Create: 73ms (aka FastMember) ObjectAccessor.Create: 92ms (aka FastMember)
CAVEAT: these are for a single test that may not be representative of your scenario. This code is shown here
So: based on a simple test, about 20-times slower than static regular C#, but about 30 times faster than reflection.
UPDATE: interesting, looks like reflection got faster in .NET 4.5:
Static C#: 13ms Dynamic C#: 249ms PropertyInfo: 2991ms PropertyDescriptor: 6761ms TypeAccessor.Create: 77ms ObjectAccessor.Create: 94ms
Here it is only about 12 times faster than reflection, because reflection got faster (not because dynamic got slower).
Therefore I want to know if using dynamic in place of object is advised and to what level this usage comes at the cost of performance?
If you don't need dynamic typing, don't use it.
If you need dynamic typing - if it avoids some complicated reflection code, for example - then use it and measure the performance cost.
The cost will heavily depend on exactly what you're doing. It will pretty much always be slower than statically typed code where the equivalent is even possible, but there are lots of factors that can affect the exact cost. As ever with performance matters, write the cleanest (not necessarily shortest) code which works to start with, measure the performance, and if it doesn't meet your performance goals, optimize carefully (with frequent measurements to check you're going in the right direction).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With