コード例 #1
0
        internal static int GetHash(Expression e)
        {
            var hashVisitor = new HashVisitor();

            hashVisitor.Visit(e);
            return(hashVisitor.Hash);
        }
コード例 #2
0
        public void Benchmark()
        {
            int n = 1000;

            Fake = new Fake {
                Next = new Level {
                    Name = "Johan"
                }
            };
            var sw = Stopwatch.StartNew();

            for (int i = 0; i < n; i++)
            {
                var h = HashVisitor.GetHash(x => Fake.Next.Name); // Warming things up
            }
            sw.Restart();
            for (int i = 0; i < n; i++)
            {
                var h = HashVisitor.GetHash(x => Fake.Next.Name);
            }
            sw.Stop();
            var t1 = sw.Elapsed;

            Console.WriteLine(
                "Getting: this.Fake.Next.Name {0} times took: {1:F1} ms {2:F4} ms per call",
                n,
                sw.Elapsed.TotalMilliseconds,
                sw.Elapsed.TotalMilliseconds / n);

            sw.Restart();
            for (int i = 0; i < n; i++)
            {
                Expression <Func <object, string> > expression = x => Fake.Next.Name;
                var h = expression.ToString();
            }
            sw.Stop();
            Console.WriteLine(
                "expression.ToString() {0} times took: {1:F1} ms {2:F4} ms per call",
                n,
                sw.Elapsed.TotalMilliseconds,
                sw.Elapsed.TotalMilliseconds / n);
        }