class Program
    {
        static void Main(string[] args)
        {
            const int iterations = 1000000;

            var stopWatch = new Stopwatch();
            

            TestClass testClass;

            FastActivator.CreateTestClass1(new[] { "Hello" }); // Warm-up
            stopWatch.Start();
            for (int i = 0; i < iterations; i++)
                testClass = new TestClass("Hello");
            stopWatch.Stop();
            Console.WriteLine("Regular call: {0} mS", stopWatch.Elapsed.TotalMilliseconds);

            stopWatch.Reset();

            var delegateCreator = FastActivator.GenerateDelegate(typeof(TestClass), typeof(string));
            stopWatch.Start();
            for (int i = 0; i < iterations; i++)
                testClass = (TestClass)delegateCreator(new object[] {"Hello"});
            stopWatch.Stop();
            Console.WriteLine("Fastactivator boxed: {0} mS", stopWatch.Elapsed.TotalMilliseconds);

            stopWatch.Reset();

            var funcCreator = FastActivator.GenerateFunc<Func<string, TestClass>>();
            stopWatch.Start();
            for (int i = 0; i < iterations; i++)
                testClass = funcCreator("Hello");
            stopWatch.Stop();
            Console.WriteLine("Fastactivator func: {0} mS", stopWatch.Elapsed.TotalMilliseconds);

            stopWatch.Reset();

            stopWatch.Start();
            for (int i = 0; i < iterations; i++)
                testClass = Activator.CreateInstance(typeof(TestClass), "Hello") as TestClass;
            stopWatch.Stop();
            Console.WriteLine("Activator: {0} mS", stopWatch.Elapsed.TotalMilliseconds);

            stopWatch.Reset();
        }
    }

Timings on my machine (for comparative purposes only)
  • Regular call: 15.7455 mS
  • Fastactivator boxed: 48.5748 mS
  • Fastactivator func: 30.5026 mS
  • Activator: 2631.9198 mS

Last edited Jan 5, 2012 at 8:40 PM by ananth, version 1

Comments

No comments yet.