typeof(String)与System.Type.GetType(“System.String”)

时间:2016-02-24 19:30:12

标签: c# .net

使用typeof(String) vs System.Type.GetType("System.String")确实有显着的性能优势吗?

如果有,我想知道原因。 尽可能深入CLR以证明它。

我的测试显示是,差不多。

第2版

结果

配置=释放

baseline: 5572 ticks 2 ms
typeof(Test): 8757 ticks 3 ms
Type.GetType(String): 3899966 ticks 1482 ms

代码

[MethodImpl(MethodImplOptions.NoInlining)]
static int Call(Type t)
{
    return 1;
}
static void Main(string[] args)
{
    const int Iterations = 1000000;
    int count;

    Stopwatch sw = Stopwatch.StartNew(); count = 0;
    for (int i = 0; i < Iterations; i++)
    {
        count += Call(null);
    }
    sw.Stop();
    Console.WriteLine("baseline: {0} ticks {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);

    sw = Stopwatch.StartNew(); count = 0;
    for (int i = 0; i < Iterations; i++)
    {
        count += Call(typeof(String));
    }
    sw.Stop();
    Console.WriteLine("typeof(Test): {0} ticks {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);

    sw = Stopwatch.StartNew(); count = 0;
    for (int i = 0; i < Iterations; i++)
    {
        count += Call(Type.GetType("System.String"));
    }
    sw.Stop();
    Console.WriteLine("Type.GetType(String): {0} ticks {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);
}

版本1

结果

配置=调试

typeof(Test): 24782 ticks 9 ms
Type.GetType(String): 4783195 ticks 1818 ms

代码

static void Main() 
{
  const int Iterations = 1000000;
  Stopwatch sw = Stopwatch.StartNew();
  for (int i = 0; i < Iterations; i++)
  {
    Type t = typeof(String);
  }
  sw.Stop();
  Console.WriteLine("typeof(Test): {0} ticks {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);

  sw = Stopwatch.StartNew();
  for (int i = 0; i < Iterations; i++)
  {
    Type t = System.Type.GetType("System.String");
  }
  sw.Stop();
  Console.WriteLine("Type.GetType(String): {0} ticks {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);
}

2 个答案:

答案 0 :(得分:8)

你回答了自己的问题。 typeof(string)更快。但看到原因很有意思。

typeof已汇编为ldtokenGetTypeFromHandle(请参阅Efficiency of C#'s typeof operator (or whatever its representation is in MSIL))。这比GetType("System.String")更有效。

另请注意,版本1 中的基准测试无效,因为未使用结果变量Type t。不使用局部变量将导致JIT优化语句。第一个循环体实际上是无操作,但第二个循环将执行。根据您报告的性能数据,这是我的猜测。

Here's a benchmark done right. NoInline函数充当您想要进行基准测试的值的接收器。缺点是您现在正在对功能调用成本进行基准测试,但它们很小。

答案 1 :(得分:2)

因为它正在做更多工作@JJS。记住你的训练并使用来源,卢克。

文档为我们提供了一些线索。 Type.GetType Method (String)

  • 如果类型在编译时程序已知的程序集中,则在C#中使用,在Visual Basic中使用GetType或在C ++中使用它会更有效。
  • 如果typeName包含命名空间但不包含程序集名称,则此方法只按顺序搜索调用对象的程序集和Mscorlib.dll。

我们知道typeof(T)是一个调用编译为ldtokenGetTypeFromHandle,但GetTypeFromHandleGetTypeByName进行比较的是什么?

让我们先解决一下这个问题。 GetTypeFromHandle定义为

[Pure]
[System.Security.SecuritySafeCritical]  // auto-generated
[ResourceExposure(ResourceScope.None)]
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern Type GetTypeFromHandle(RuntimeTypeHandle handle);

让我们得到一个我们可以参考的CLR版本。

Shared Source Common Language Infrastructure 2.0 Release

runtimehandles.cpp

FCIMPL1(Object*, RuntimeTypeHandle::GetRuntimeType, void* th) { 
    CONTRACTL {
        THROWS;
        DISABLED(GC_TRIGGERS);
        MODE_COOPERATIVE;
        SO_TOLERANT;
    }
    CONTRACTL_END;

    OBJECTREF refType = NULL;

    TypeHandle typeHandle = TypeHandle::FromPtr(th);
    TypeHandle* pTypeHandle = &typeHandle;

    _ASSERTE(CheckPointer(pTypeHandle));
    _ASSERTE(CheckPointer(pTypeHandle->AsPtr(), NULL_OK));

    if (pTypeHandle->AsPtr() == NULL)
        return NULL;

    refType = pTypeHandle->GetManagedClassObjectIfExists();

    if (refType != NULL)
        return OBJECTREFToObject(refType);

    HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_RETURNOBJ, refType);

    refType = pTypeHandle->GetManagedClassObject();
    HELPER_METHOD_FRAME_END();

    return OBJECTREFToObject(refType);
}
FCIMPLEND

好的。这是合法的。我们在这里做一个邪恶的简单调用来获得一个OBJECTREFToObject。

无需搜索,只需通过方法表有效查找类型。需要在.Net internals上进行复习吗?

好的,缓慢的方法怎么样? Type.GetType Method (String)

追逐调用堆栈,发现它调用了RuntimeTypeHandle.GetTypeByName

[System.Security.SecurityCritical]  // auto-generated
[ResourceExposure(ResourceScope.None)]
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
[SuppressUnmanagedCodeSecurity]
private extern static void GetTypeByName(string name, bool throwOnError, bool ignoreCase, bool reflectionOnly, StackCrawlMarkHandle stackMark, 
#if FEATURE_HOSTED_BINDER
IntPtr pPrivHostBinder,
#endif
bool loadTypeFromPartialName, ObjectHandleOnStack type);

runtimehandles.cpp

FCIMPL6(EnregisteredTypeHandle, RuntimeTypeHandle::GetTypeByName, 
    StringObject* classNameUNSAFE, CLR_BOOL bThrowOnError, CLR_BOOL bIgnoreCase, CLR_BOOL bReflectionOnly, StackCrawlMark* pStackMark, CLR_BOOL bLoadTypeFromPartialNameHack) 
{
    CONTRACTL 
    {
        THROWS;
        DISABLED(GC_TRIGGERS);
        MODE_COOPERATIVE;
        SO_TOLERANT;
    }
    CONTRACTL_END;

    STRINGREF sRef = (STRINGREF) classNameUNSAFE;
    TypeHandle typeHandle;

    HELPER_METHOD_FRAME_BEGIN_RET_1(sRef);
    {
        if (!sRef)
            COMPlusThrowArgumentNull(L"className",L"ArgumentNull_String");

        typeHandle = TypeName::GetTypeManaged(sRef->GetBuffer(), NULL, bThrowOnError, bIgnoreCase, bReflectionOnly, /*bProhibitAsmQualifiedName =*/ FALSE, pStackMark, bLoadTypeFromPartialNameHack);        
    }
    HELPER_METHOD_FRAME_END();

    return typeHandle.AsPtr();
}
FCIMPLEND

很好,但TypeName :: GetTypeManaged在做什么?!

typeparse.cpp

//--------------------------------------------------------------------------------------------------------------
// This everything-but-the-kitchen-sink version is what used to be called "GetType()". It exposes all the
// funky knobs needed for implementing the specific requirements of the managed Type.GetType() apis and friends.
//--------------------------------------------------------------------------------------------------------------
/*public static */ TypeHandle TypeName::GetTypeManaged

但它并不止于此

typeparse.cpp

// -------------------------------------------------------------------------------------------------------------
// This is the "uber" GetType() that all public GetType() funnels through. It's main job is to figure out which
// Assembly to load the type from and then invoke GetTypeHaveAssembly.
//
// It's got a highly baroque interface partly for historical reasons and partly because it's the uber-function
// for all of the possible GetTypes.
// -------------------------------------------------------------------------------------------------------------
/* private instance */ TypeHandle TypeName::GetTypeWorker

也不止于此。

typeparse.cpp

//----------------------------------------------------------------------------------------------------------------
// This is the one that actually loads the type once we've pinned down the Assembly it's in.
//----------------------------------------------------------------------------------------------------------------
/* private instance */ TypeHandle TypeName::GetTypeHaveAssembly(Assembly* pAssembly, BOOL bThrowIfNotFound, BOOL bIgnoreCase, BOOL bRecurse)

for (COUNT_T i = 0; i < names.GetCount(); i ++)
{
    LPCWSTR wname = names[i]->GetUnicode();
    MAKE_UTF8PTR_FROMWIDE(name, wname);
    typeName.SetName(name);       
    th = pAssembly->GetLoader()->LoadTypeHandleThrowing(&typeName);
}

clsload.cpp

TypeHandle ClassLoader::LoadTypeHandleThrowing(NameHandle* pName, ClassLoadLevel level, Module* pLookInThisModuleOnly/*=NULL*/)

    BOOL foundSomething = FindClassModuleThrowing(pName,

// FindClassModuleThrowing发现您要查找的类型所在的模块,并在必要时加载模块。 //基本上,它遍历所有程序集的模块,直到在模块中找到名称匹配 // AvailableClassHashTable。

    if (!typeHnd.IsNull()) {
        typeHnd = LoadTypeDefThrowing(typeHnd.GetModule(), typeHnd.GetCl(),

//给定一个指定typeDef的标记,以及一个指定其中的模块 //解释该标记,查找或加载相应的类型句柄。

typeHnd = pModule->LookupTypeDef(typeDef, level);

ceeload.h

TypeHandle LookupTypeDef(mdTypeDef token, ClassLoadLevel level = CLASS_LOAD_UNRESTOREDTYPEKEY)

给了我们TypeHandle。我们在GetTypeFromHandle

的单个堆栈框架中得到了同样的东西
    PTR_MethodTable pMT = PTR_MethodTable(GetFromRidMap(&m_TypeDefToMethodTableMap, RidFromToken(token)));
    if (pMT == NULL || pMT->GetLoadLevel() < level)
        return TypeHandle();
    else
        return (TypeHandle)pMT;

那么......什么慢? FindClassModuleThrowing中的迭代。它必须迭代遍历名称以查找方法表...迭代数组,总是比通过已知密钥查找某些内容更慢,GetTypeFromHandle

案件结案。