数据优化学习 1

今天看了 codeproject 上的一片文章(http://www.codeproject.com/useritems/C__Code_profiling.asp),知道了自己进行控件测试最原始的方法。

按照上面的方法对几个常用的类进行了分析,分析数据如下:

ArrayList
1 2 3 4 5 6 7 8 9 10 平均值
ForMethod:  20 20 10 20 21 30 20 20 20 20 20 ms
ForCountMethod:  20 20 20 20 10 30 20 20 10 20 19 ms
ForEachMethod:  50 40 40 40 40 40 40 40 40 40 41 ms
IEnumeratorMethod:  40 40 40 30 30 50 40 41 40 40 39 ms
Hashtalbe
1 2 3 4 5 6 7 8 9 10
ForMethod:  251 491 240 421 291 401 270 290 250 251 316 ms
ForCountMethod:  160 160 171 160 170 160 160 150 160 160 161 ms
IDEnumeratorMethod 50 50 50 50 50 60 50 50 50 50 51 ms
String[]
ForMethod:  451 440 440 440 441 491 441 441 440 440 447 ms
ForCountMethod:  471 441 450 451 450 521 450 470 451 441 460 ms
IEnumeratorMethod:  620 581 581 581 591 731 601 591 601 591 607 ms
ForEachMethod:  461 451 441 450 451 521 451 441 440 450 456 ms

[说明:测试数据为 1000000。时间单位: ms] 

总结:

1、对于 ArrayList  最好采用如下循环读取数据比较快:

int total = arrayList.Count;

for(int iCount = 0;iCount < total; iCount ++)

{

    // many of codes...

}

2、对于 Hashtable 循环读取数据,推荐方法:

IDictionaryEnumerator ide = ht.GetEnumerator();

while (ht.MoveNext())

{

// many of codes...

}

3、对于 String[] 之类的引用类型循环读取数据,推荐:

foreach(int str in arrayStr)

{

// many of codes...

}

原文地址:https://www.cnblogs.com/AloneSword/p/2237553.html