C# 循环中 直接索引 VS 缓存索引 性能测试
2024-10-19 10:03:47
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 = ;
} public static int stimes = ; public static void Main (string[] args)
{
int times = stimes;
t1 test1 = new t1();
DateTime s1 = DateTime.Now;
for (int i = ; i < times; 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 = ; i < times; 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 = ; i < times; i++)
{
b3.x = b3.x + ;
}
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 = ; i < stimes; i++)
{
b4.x = b4.x + ;
}
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 = ; i < ; i++)
{
b5.x = b5.x + ;
}
DateTime e5 = DateTime.Now;
Console.WriteLine("time5 = " + (e5 - s5).Milliseconds);
} }
}
output:
time1 = 456
time2 = 144
time3 = 145
time4 = 145
time5 = 179
请按任意键继续. . .
总结: 缓存一下总是有好处的
最新文章
- Python数据结构与算法设计总结篇
- 在CTabCtrl上动态创建CListCtrl控件
- socket编写简单回显server
- 解决ssh或ftp下root用户认证失败问题
- Linux变量内容的删除、取代与替换
- .NET开发微信小程序-Template模块开发
- JS 超类和子类
- js 连接mqtt
- Python学习之MacBook Pro中Python3.7安装pip以及numpy
- spring boot+mybatis+generator生成domain大小写问题
- jquery的设计亮点
- PAT A1132 Cut Integer (20 分)——数学题
- linux如何删除行首的空格
- i2c驱动笔记
- “Hello World!”团队第六周的第二次会议
- HTML5页面CSS Reset
- include和application
- P1297 网线切割
- linux fg bg ctrl + z jobs &; 等命令
- ACM学习历程—51NOD1028 大数乘法V2(FFT)