Re: [aus-dotnet] GetType or Type.GetType


    [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
    • From: Piers Williams
    • Subject: Re: [aus-dotnet] GetType or Type.GetType
    • Date: Sat, 29 Oct 2005 17:24:59 +1000

    
    
    On 10/25/05, Matt wrote:
    
    Does anyone know if is slower using Type.GetType(string) over
    GetType(type) when using .Net types? (not user defined)
    
    I'm guessing there would be as the Type class would be using a lot of
    checking but i'm just too lazy to write a test and thought some guru
    might know.
    
    you'd expect a fairly big difference, and there is: nearly 100x:
    
    GetType() : 2106
    object.GetType() : 2982
    Type.GetType(string) : 139757
    
    Second pass
    GetType() : 1326
    object.GetType() : 2895
    Type.GetType(string) : 127049
    
    [10000 iterations; ignore the units, just consider the ratios]
    
    
    Bear in mind that Type.GetType(string) has got all kinds of work to do. Even in the simplest case - you just specify a type name - it's pretty much got to scan all the in-memory structures for the current assembly and see if they match the type name you've provided (I guess there might be a name -> type handle lookup map to optimize this, but...). Worse, if you provide a fully qualified type name, and if the referenced assembly's not been loaded yet, it's going to go out to the CLR loader, which might start poking about in the GAC. In both cases the type name has to be parsed to work out what you wanted. You see where this is going.
    
    
    what's interesting is the difference between using the GetType() function (typeof in C#) and object.GetType(). I thought they were pretty much the same thing, but GetType() / typeof() gets to shortcut all the polymorphic mucking about in object.GetType() by using an internal pointer directly to the type handle:
    
    IL (same for VB / C#)
    
    GetType() / typeof()
     IL_000d:  ldtoken    [mscorlib]System.String
    
    IL_0012: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    
    object.GetType()
     IL_0045:  ldsfld     string [mscorlib]System.String::Empty
    
    IL_004a: callvirt instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
    
    Type.GetType(string)
     IL_007d:  ldstr      "System.String"
    
    IL_0082: call class [mscorlib]System.Type [mscorlib]System.Type::GetType(string)
    
    
    
    so I guess the moral is to always use GetType() / typeof() if you've got the option. But you'd be doing that anyway (right?), because then you can't make a typo. If you're late binding for architectural (decoupling / plugin) reasons then the performance hit is irrelevant anyway (it's not like we're talking about a 'put the kettle on' type delay)
    
    --
    piers
    senior developer, UnisysWest
    
    _________________
    
    You are a part of the Ausralian "dotnet" mailing list. To unsubscribe send "unsubscribe dotnet" or to re-subscribe send "subscribe dotnet" in the body of the email to: imailsrv@xxxxxxxxxxx List Managed by www.stanski.com and Proudly Sponsored by www.ico.com.au
    
    



    (Click here for more information on the aus-dotnet mailling list)