Example #1
0
        public static void Main()
        {
            var test = "Test";

            Expression <Func <Cat, string> > expression = cat => cat.SayMew(test);

            var list = new List <string>();

            var stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < 10000; i++)
            {
                var body     = expression.Body as MethodCallExpression;
                var argument = body.Arguments[0];

                // () => (object)test;
                var converted = Expression.Convert(argument, typeof(object));
                var lambda    = Expression.Lambda <Func <object> >(converted);

                var func = lambda.Compile();

                list.Add(func() as string);
            }

            Console.WriteLine($"{stopwatch.Elapsed} - Extracting with normal expression and convert");
            Console.WriteLine(list.Count);

            list = new List <string>();

            stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < 10000; i++)
            {
                var body     = expression.Body as MethodCallExpression;
                var argument = body.Arguments[0];

                // () => (object)test;
                var converted = Expression.Convert(argument, typeof(object));
                var lambda    = Expression.Lambda <Func <object> >(converted);

                var func = lambda.CompileFast();

                list.Add(func() as string);
            }

            Console.WriteLine($"{stopwatch.Elapsed} - Extracting with fast compiled expression and convert");
            Console.WriteLine(list.Count);

            list = new List <string>();

            stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < 10000; i++)
            {
                var body     = expression.Body as MethodCallExpression;
                var argument = body.Arguments[0];

                var argumentMember    = argument as MemberExpression;
                var closureClass      = argumentMember.Expression as ConstantExpression;
                var closureClassValue = closureClass.Value;

                var fieldInfo = argumentMember.Member as FieldInfo;
                var value     = fieldInfo.GetValue(closureClassValue);

                list.Add(value as string);
            }

            Console.WriteLine($"{stopwatch.Elapsed} - Extracting with reflection");
            Console.WriteLine(list.Count);

            list = new List <string>();

            var bodyCold     = expression.Body as MethodCallExpression;
            var argumentCold = bodyCold.Arguments[0];

            var argumentMemberCold    = argumentCold as MemberExpression;
            var closureClassCold      = argumentMemberCold.Expression as ConstantExpression;
            var closureClassValueCold = closureClassCold.Value;

            MemberHelper.GetMembers(closureClassValueCold.GetType());

            stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < 10000; i++)
            {
                var body     = expression.Body as MethodCallExpression;
                var argument = body.Arguments[0];

                var argumentMember    = argument as MemberExpression;
                var closureClass      = argumentMember.Expression as ConstantExpression;
                var closureClassValue = closureClass.Value;

                var members = MemberHelper.GetMembers(closureClassValue.GetType());

                var value = members[0].Getter(closureClassValue);

                list.Add(value as string);
            }

            Console.WriteLine($"{stopwatch.Elapsed} - Extracting with reflection and compiled delegates");
            Console.WriteLine(list.Count);

            list = new List <string>();

            stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < 10000; i++)
            {
                var slowExpression = new SlowExpression();

                var value = slowExpression.BuildSlowExpression();

                list.Add(value);
            }

            Console.WriteLine($"{stopwatch.Elapsed} - Building with slow expression");
            Console.WriteLine(list.Count);

            list = new List <string>();

            stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < 10000; i++)
            {
                var slowExpression = new SlowExpressionWithFastCompile();

                var value = slowExpression.BuildSlowExpression();

                list.Add(value);
            }

            Console.WriteLine($"{stopwatch.Elapsed} - Building with slow expression and fast compile");
            Console.WriteLine(list.Count);

            list = new List <string>();

            stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < 10000; i++)
            {
                var slowExpression = new FastExpression();

                var value = slowExpression.BuildFastExpression();

                list.Add(value);
            }

            Console.WriteLine($"{stopwatch.Elapsed} - Building with fast expression");
            Console.WriteLine(list.Count);
        }
Example #2
0
        static void Main(string[] args)
        {
            var text = "Use expression trees";
            Expression <Func <Cat, string> > sayMewExpression = cat => cat.SayMew(text);
            List <string> list = new List <string>();

            Stopwatch stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < OperationsCount; i++)
            {
                MethodCallExpression body     = sayMewExpression.Body as MethodCallExpression;
                Expression           argument = body.Arguments[0];

                // () => (object)test;
                UnaryExpression             converted = Expression.Convert(argument, typeof(object));
                Expression <Func <object> > lambda    = Expression.Lambda <Func <object> >(converted);
                Func <object> func = lambda.Compile();

                string value = func() as string;
                list.Add(value);
            }

            Console.WriteLine($"{stopwatch.Elapsed} - Extracting using normal expression compiler");

            list      = new List <string>();
            stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < OperationsCount; i++)
            {
                MethodCallExpression body     = sayMewExpression.Body as MethodCallExpression;
                Expression           argument = body.Arguments[0];

                // () => (object)test;
                UnaryExpression             converted = Expression.Convert(argument, typeof(object));
                Expression <Func <object> > lambda    = Expression.Lambda <Func <object> >(converted);
                Func <object> func = lambda.CompileFast();

                string value = func() as string;
                list.Add(value);
            }

            Console.WriteLine($"{stopwatch.Elapsed} - Extracting using fast expression compiler");

            list      = new List <string>();
            stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < OperationsCount; i++)
            {
                MethodCallExpression body     = sayMewExpression.Body as MethodCallExpression;
                Expression           argument = body.Arguments[0];

                MemberExpression   arguemntMemeber        = argument as MemberExpression;
                ConstantExpression closureClassExpression = arguemntMemeber.Expression as ConstantExpression;
                object             closureClass           = closureClassExpression.Value;

                FieldInfo clasureClassFieldInfo = arguemntMemeber.Member as FieldInfo;
                string    value = (string)clasureClassFieldInfo.GetValue(closureClass);
                list.Add(value);
            }

            Console.WriteLine($"{stopwatch.Elapsed} - Extracting with reflection");

            list      = new List <string>();
            stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < OperationsCount; i++)
            {
                string value = NormalExpression.BuildExpression();
                list.Add(value);
            }

            Console.WriteLine($"{stopwatch.Elapsed} - Building with normal expression");

            list      = new List <string>();
            stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < OperationsCount; i++)
            {
                string value = FastExpression.BuildExpression();
                list.Add(value);
            }

            Console.WriteLine($"{stopwatch.Elapsed} - Building with normal expression and using fast compile");
        }