Performance cost of using `dynamic` vs `object`?

15,677

Solution 1

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).

Solution 2

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).

Share:
15,677

Related videos on Youtube

Matthew Layton
Author by

Matthew Layton

Passionate about programming, technology and DLT.

Updated on June 15, 2022

Comments

  • Matthew Layton
    Matthew Layton about 2 years

    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?

  • enzi
    enzi over 8 years
    Could you clarify the Dynamic C# test: did you measure the time for the first call, or a subsequent (cached) one? I assume the former and that subsequent calls are a lot faster, but not sure. (I know I could test it myself, but I believe future readers would also benefit from the clarification ;)