コード例 #1
0
        private static Func <TReturn> CreateLCGApproach <TArg, TReturn>(Func <TArg, TReturn> f, TArg arg)
        {
            if (f.Target != null)
            {
                throw new ArgumentException();
            }

            var factory = FactoryMethodHelper <TArg, TReturn> .GetFactory(f.Method);

            Func <TReturn> simpleWrapper = factory(arg);

            return(simpleWrapper);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var f       = new Func <string, string>(Extensions.SomeAction);
            var ctor    = typeof(Func <string>).GetConstructors().Single();
            var factory = FactoryMethodHelper <string, string> .GetFactory(f.Method);

            const int iterations = 50000;

            var ctorTimer            = new Stopwatch();
            var delegateTimer        = new Stopwatch();
            var baselineTimer        = new Stopwatch();
            var lcgTimer             = new Stopwatch();
            var lcgPreFactoryTimer   = new Stopwatch();
            var extensionMethodTimer = new Stopwatch();

            for (int i = 0; i < iterations; i++)
            {
                ctorTimer.Start();
                string result = A1(f, ctor);
                ctorTimer.Stop();

                delegateTimer.Start();
                result = A2(f);
                delegateTimer.Stop();

                baselineTimer.Start();
                result = A3(f);
                baselineTimer.Stop();

                lcgTimer.Start();
                result = A4(f);
                lcgTimer.Stop();

                lcgPreFactoryTimer.Start();
                result = A5(f, factory);
                lcgPreFactoryTimer.Stop();

                extensionMethodTimer.Start();
                result = A6();
                extensionMethodTimer.Stop();
            }

            Console.WriteLine("Func<>.ctor: {0}", ctorTimer.ElapsedMilliseconds);
            Console.WriteLine("Delegate:    {0}", delegateTimer.ElapsedMilliseconds);
            Console.WriteLine("Baseline:    {0}", baselineTimer.ElapsedMilliseconds);
            Console.WriteLine("LCG:         {0}", lcgTimer.ElapsedMilliseconds);
            Console.WriteLine("LCG (pref):  {0}", lcgPreFactoryTimer.ElapsedMilliseconds);
            Console.WriteLine("Ext. method: {0}", extensionMethodTimer.ElapsedMilliseconds);
        }