I was benchmarking some type instantiation code, and I stumbled upon this:

when I instantiate in a loop 1,000,000 times a class with the code:

MyType t = Activator.CreateInstance<MyType>();

it takes 2800ms or thereabout.

When I use:
MyType t = (MyType)Activator.CreateInstance(typeof(MyType));

it takes 330ms or thereabout

(debug builds).

why this big difference? The code internally (peeking with reflector) shows
two different code paths, though I then wonder, why isn't
Activator.CreateInstance<T> implemented as:

public T CreateInstance<T>()
{
        return (T)Activator.CreateInstance(typeof(T));
}

?

        FB

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to