Sébastien Lorion <[EMAIL PROTECTED]> wrote:

> Funny, I get exactly the inverse behavior both in debug and release ... ?!

>     class MyReferenceType

Your results appear to be anomalous because typeof(T) where T is a
non-public type invokes more security checking. I get the following
results:

Activator.Create(typeof(Public))   :  0.147 sec with 1000000 reps
Activator.Create<Public>()         :  1.035 sec with 1000000 reps
Activator.Create(typeof(Private))  :  1.981 sec with 1000000 reps
Activator.Create<Private>()        :  1.029 sec with 1000000 reps

... using this test program:

---8<---
using System;
using System.Diagnostics;

public class Public { public string field1; }
class Private { public string field1; }

public class App
{
    delegate void Proc();
    const int Repetitions = 1000000;
    
    static void Time(string label, Proc proc)
    {
        Stopwatch w = Stopwatch.StartNew();
        for (int i = 0; i < Repetitions; ++i)
            proc();
        Console.WriteLine("{0,-35}: {1,6:f3} sec with {2} reps", label,
            w.ElapsedTicks / (double) Stopwatch.Frequency,
            Repetitions);
    }
    
    static void Main()
    {
        Time("Activator.Create(typeof(Public))", delegate
        {
            Public item = (Public)
Activator.CreateInstance(typeof(Public));
        });
        Time("Activator.Create<Public>()", delegate
        {
            Public item = Activator.CreateInstance<Public>();
        });
        Time("Activator.Create(typeof(Private))", delegate
        {
            Private item = (Private)
Activator.CreateInstance(typeof(Private));
        });
        Time("Activator.Create<Private>()", delegate
        {
            Private item = Activator.CreateInstance<Private>();
        });
    }
}
--->8---

-- Barry

-- 
http://barrkel.blogspot.com/

===================================
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