常用测试数据。

先有一个关于 Try Catch 的文章:http://www.cnblogs.com/wlb/archive/2009/12/18/1626816.html

我所考虑的问题是,在什么数量级上,算是性能瓶颈。暂不考虑IO,CPU,内存的占用率。

关于反射,公认的,反射是影响性能的。大家喜欢用,而避免滥用。

以 万次 循环为例 (测试环境: OpenSuse11.2 64 Mono2.6 ,Core2 8300 2.4 2G ):

 

Test test =new Test() ;
test.Str
="hello" ;

System.Diagnostics.Stopwatch sw
=new System.Diagnostics.Stopwatch() ;
sw.Start() ;
for (int i =0; i <10000; i++) {
test.GetType().GetProperty(
"Str") ;
}
sw.Stop() ;
Console.WriteLine (sw.ElapsedMilliseconds.ToString());

结果:
8-16 , 平均: 12

 

 


Test test
=new Test() ;
test.Str
="hello" ;

System.Diagnostics.Stopwatch sw
=new System.Diagnostics.Stopwatch() ;
sw.Start() ;
for (int i =0; i <10000; i++) {
test.GetType().GetProperty(
"Str").GetValue(test,null) ;
}
sw.Stop() ;
Console.WriteLine (sw.ElapsedMilliseconds.ToString());

结果15
-21 , 平均:18

 


System.Diagnostics.Stopwatch sw
=new System.Diagnostics.Stopwatch() ;
sw.Start() ;
for (int i =0; i <10000; i++) {
DateTime dt
= DateTime.Now ;
}
sw.Stop() ;
Console.WriteLine (sw.ElapsedMilliseconds.ToString());

结果:
20-30,平均:25.

 


Test test
=new Test() ;
test.Str
="hello" ;

System.Diagnostics.Stopwatch sw
=new System.Diagnostics.Stopwatch() ;
sw.Start() ;
for (int i =0; i <10000; i++) {
test.GetType().GetProperty(
"Str").SetValue(test,i.ToString(),null) ;
}
sw.Stop() ;
Console.WriteLine (sw.ElapsedMilliseconds.ToString());

结果:
3040,平均: 35.

 


Test test
=new Test() ;
test.Str
="hello" ;

System.Diagnostics.Stopwatch sw
=new System.Diagnostics.Stopwatch() ;
sw.Start() ;
for (int i =0; i <10000; i++) {
test.GetType().GetMethod(
"Mm").Invoke(test,newobject[]{i}) ;
}
sw.Stop() ;
Console.WriteLine (sw.ElapsedMilliseconds.ToString());


//
publicclass Test
{
publicstring Str {get;set;}
publicstring Mm(int i)
{
return i.ToString() +"!" ;
}
}

结果:
3040 平均: 35

 


System.Diagnostics.Stopwatch sw
=new System.Diagnostics.Stopwatch() ;
sw.Start() ;

decimal m =decimal.MaxValue ;
for (int i =0; i <10000; i++) {
try{
int w = Convert.ToInt32( m ) ;
}
catch{
}
}
sw.Stop() ;
Console.WriteLine (sw.ElapsedMilliseconds.ToString());


执行时间为:
40-60, 平均 50

 



System.Diagnostics.Stopwatch sw
=new System.Diagnostics.Stopwatch() ;
sw.Start() ;
for (int i =0; i <10000; i++) {
new Random() ;
}
sw.Stop() ;
Console.WriteLine (sw.ElapsedMilliseconds.ToString());

执行时间:
60-70, 平均: 65

 

 

调用方法 Mono 。NET
GetType().GetProperty 12 5
GetType().GetProperty("Str").GetValue 18 13
DateTime.Now 25 5
GetType().GetProperty("Str").SetValue 35 21
GetType().GetMethod("Mm").Invoke 35 19
Convert.ToInt32 50 622
Random 65 58

 

 

个人认为,标准线应该在 DateTime.Now 上。 即: 万次执行小于 25 毫秒 可以不考虑。

posted @ 2010-01-26 15:44  NewSea  阅读(531)  评论(0)    收藏  举报