コード例 #1
0
        public static void TestSetPropertyValue(int times)
        {
            var foo1         = new Foo1();
            var foo2         = new Foo2();
            var foo3         = new Foo3();
            var bar          = new Bar();
            var property     = typeof(IFoo).GetProperty("Bar");
            var setAction    = CreateSetPropertyValueAction();
            var setDelegate1 = CreateSetPropertyValueDelegate(foo1);
            var setDelegate2 = CreateSetPropertyValueDelegate(foo2);
            var setDelegate3 = CreateSetPropertyValueDelegate(foo3);
            var setemit1     = CreateSetPropertyValueEmit();
            var setemit2     = CreateSetPropertyValueEmit();
            var setemit3     = CreateSetPropertyValueEmit();

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int i = 0; i < times; i++)
            {
                property.SetValue(foo1, bar, null);
                property.SetValue(foo2, bar, null);
                property.SetValue(foo3, bar, null);
            }
            var duration1 = stopwatch.ElapsedMilliseconds;

            stopwatch.Restart();
            for (int i = 0; i < times; i++)
            {
                setAction(foo1, bar);
                setAction(foo2, bar);
                setAction(foo3, bar);
            }
            var duration2 = stopwatch.ElapsedMilliseconds;

            stopwatch.Restart();
            for (int i = 0; i < times; i++)
            {
                setDelegate1(bar);
                setDelegate2(bar);
                setDelegate3(bar);
            }
            var duration3 = stopwatch.ElapsedMilliseconds;

            stopwatch.Restart();
            for (int i = 0; i < times; i++)
            {
                setemit1(foo1, new Bar());
                setemit2(foo2, new Bar());
                setemit3(foo3, new Bar());
            }
            var duration4 = stopwatch.ElapsedMilliseconds;

            Console.WriteLine("{0, -15}{1,-15}{2,-15}{3,-15}{4,-15}", times, duration1, duration2, duration3, duration4);
        }
コード例 #2
0
        public static void TestGetPropertyValue(int times)
        {
            var foo1 = new Foo1 {
                Bar = new Bar()
                {
                    Name = "zhansghan"
                }
            };
            var foo2 = new Foo2 {
                Bar = new Bar()
                {
                    Name = "lisi"
                }
            };
            var foo3 = new Foo3 {
                Bar = new Bar()
                {
                    Name = "aa"
                }
            };

            var property     = typeof(IFoo).GetProperty("Bar");
            var getFunc      = CreateGetPropertyValueFunc();
            var getDelegate1 = CreateGetPropertyValueDelegate(foo1);
            var getDelegate2 = CreateGetPropertyValueDelegate(foo2);
            var getDelegate3 = CreateGetPropertyValueDelegate(foo3);

            var getemit1 = CreateGetPropertyValueEmit();
            var getemit2 = CreateGetPropertyValueEmit();
            var getemit3 = CreateGetPropertyValueEmit();

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int i = 0; i < times; i++)
            {
                var bar1 = property.GetValue(foo1, null);
                var bar2 = property.GetValue(foo2, null);
                var bar3 = property.GetValue(foo3, null);
            }
            var duration1 = stopwatch.ElapsedMilliseconds;

            stopwatch.Restart();
            for (int i = 0; i < times; i++)
            {
                var bar1 = getFunc(foo1);
                var bar2 = getFunc(foo2);
                var bar3 = getFunc(foo3);
            }
            var duration2 = stopwatch.ElapsedMilliseconds;

            stopwatch.Restart();
            for (int i = 0; i < times; i++)
            {
                var bar1 = getDelegate1();
                var bar2 = getDelegate2();
                var bar3 = getDelegate3();
            }
            var duration3 = stopwatch.ElapsedMilliseconds;

            stopwatch.Restart();
            for (int i = 0; i < times; i++)
            {
                var bar1 = getemit1(foo1);
                var bar2 = getemit2(foo2);
                var bar3 = getemit3(foo3);
            }
            var duration4 = stopwatch.ElapsedMilliseconds;

            Console.WriteLine("{0, -15}{1,-15}{2,-15}{3,-15}{4,-15}", times, duration1, duration2, duration3, duration4);
        }