C# 循环中 直接索引 VS 缓存索引 性能测试

2021-02-19 08:18

阅读:732

标签:line   name   ++   console   arp   div   rgs   system   测试   

using System;

namespace TestCSharp
{
    class MainClass
    {
        public class t1 {
            public b1 b = new b1();
        }
        
        public class b1 {
            public t2 t = new t2();
        }
        
        public class t2 {
            public b2 b = new b2();
        }
        
        public class b2 {
            public t3 t = new t3();
        }
        
        public class t3 {
            public b3 b = new b3();
        }
        
        public class b3 {
            public t4 t = new t4();
        }
        
        public class t4 {
            public b4 b = new b4();
        }
        
        public class b4 {
            public t5 t = new t5();
        }
        
        public class t5 {
            public b5 b = new b5();
        }
        
        public class b5 {
            public t6 t = new t6();
        }
        
        public class t6 {
            public b6 b = new b6();
        }
        
        public class b6 {
            public int x = 0;
        }

        public static int stimes = 100000000;

        public static void Main (string[] args)
        {
            int times = stimes;
            t1 test1 = new t1();
            DateTime s1 = DateTime.Now;
            for (int i = 0; i )
            {
                test1.b.t.b.t.b.t.b.t.b.t.b.x ++;
            }
            DateTime e1 = DateTime.Now;
            Console.WriteLine("time1 = " + (e1 - s1).Milliseconds);

            t1 test2 = new t1();
            DateTime s2 = DateTime.Now;
            b6 b2 = test2.b.t.b.t.b.t.b.t.b.t.b;
            for (int i = 0; i )
            {
                b2.x ++;
            }
            DateTime e2 = DateTime.Now;
            Console.WriteLine("time2 = " + (e2 - s2).Milliseconds);

            t1 test3 = new t1();
            DateTime s3 = DateTime.Now;
            b6 b3 = test3.b.t.b.t.b.t.b.t.b.t.b;
            for (int i = 0; i )
            {
                b3.x = b3.x + 1;
            }
            DateTime e3 = DateTime.Now;
            Console.WriteLine("time3 = " + (e3 - s3).Milliseconds);

            t1 test4 = new t1();
            DateTime s4 = DateTime.Now;
            b6 b4 = test4.b.t.b.t.b.t.b.t.b.t.b;
            for (int i = 0; i )
            {
                b4.x = b4.x + 1;
            }
            DateTime e4 = DateTime.Now;
            Console.WriteLine("time4 = " + (e4 - s4).Milliseconds);

            t1 test5 = new t1();
            DateTime s5 = DateTime.Now;
            b6 b5 = test5.b.t.b.t.b.t.b.t.b.t.b;
            for (int i = 0; i 100000000; i++)
            {
                b5.x = b5.x + 1;
            }
            DateTime e5 = DateTime.Now;
            Console.WriteLine("time5 = " + (e5 - s5).Milliseconds);
        }



    }
}

output:

time1 = 456
time2 = 144
time3 = 145
time4 = 145
time5 = 179
请按任意键继续. . .

总结: 缓存一下总是有好处的

C# 循环中 直接索引 VS 缓存索引 性能测试

标签:line   name   ++   console   arp   div   rgs   system   测试   

原文地址:https://www.cnblogs.com/xixidaguai/p/8321215.html


评论


亲,登录后才可以留言!