奇了怪了..

泛型集合类的优势好象还是类型安全,性能方面好象没什么优势

下面那个测试写得有点急,写错了。

但不可理解的是明明ArrayList比List<T>多一步装箱操作,但测试结果确不能让人满意。

 

class  Program
    
{
        
static void Main(string[] args)
        
{
            Console.WriteLine(
"Constructor Time:");
            Program.TestConstruct();
            Console.WriteLine();
            Console.WriteLine(
"AddMechod Time:");
            Program.TestAddMethod();
            Console.WriteLine();
            Console.WriteLine(
"Loop Add Time:");
            Program.Test(
10);
            Program.Test(
100);
            Program.Test(
1000);
            Program.Test(
1000 * 1000);
            Program.Test(Int32.MaxValue);
            Console.ReadLine();
        }


        
private static void Test(int times)
        
{
            SomeValueTye temp; 
            IList
<SomeValueTye> list = new List<SomeValueTye>();
            ArrayList al 
= new ArrayList();
            Stopwatch sw 
= new Stopwatch();  
          
            
//List<T>,add操作
            sw.Start();           
            
for (int i = 0; i < times; i++)
            
{
                list.Add(
new SomeValueTye(i, "test"));
            }

            sw.Stop();
            Console.WriteLine(
"IL In"  + " " + sw.Elapsed + " " + times.ToString());
            
            sw.Reset();
            
            
//ArrayList, add操作
            sw.Start();            
            
for (int i = 0; i < times; i++)
            
{
                al.Add(
new SomeValueTye(i, "test"));
            }
            
            sw.Stop();
            Console.WriteLine(
"AL In" + " " + sw.Elapsed + " " + times.ToString());

            sw.Reset();

            
//List<T>,取值
            sw.Start();
            
for (int i = 0; i < times; i++)
            
{
                temp 
= list[i];
            }

            sw.Stop();
            Console.WriteLine(
"IL Out" + " " + sw.Elapsed + " " + times.ToString());

            sw.Reset();

            
//ArrayList,取值
            sw.Start();
            
for (int i = 0; i < times; i++)
            
{
                temp 
= (SomeValueTye)al[i];
            }

            sw.Stop();
            Console.WriteLine(
"AL Out" + " " + sw.Elapsed + " " + times.ToString());
            Console.WriteLine();
        }


        
private static void TestConstruct()
        
{
            Stopwatch sw 
= new Stopwatch();           
            sw.Start();
            
for (int i = 0; i < 1000 * 1000; i++)
            
{
                IList
<SomeValueTye> list = new List<SomeValueTye>(10);
            }

            sw.Stop();
            Console.WriteLine(
"IL: " + sw.Elapsed);

            sw.Reset();

            sw.Start();
            
for (int i = 0; i < 1000 * 1000; i++)
            
{
                ArrayList al 
= new ArrayList(10);
            }

            sw.Stop();
            Console.WriteLine(
"AL: " + sw.Elapsed);
        }


        
private static void TestAddMethod()
        
{
            Stopwatch sw 
= new Stopwatch();            
            IList
<SomeValueTye> list = new List<SomeValueTye>();
            sw.Start();
            
for(int i = 0; i < 1000*1000; i++)
                list.Add(
new SomeValueTye(1,"test"));
            sw.Stop();
            Console.WriteLine(
"IL: " + sw.Elapsed);

            sw.Reset();   
         
            ArrayList al 
= new ArrayList();
            sw.Start();
            
for (int i = 0; i < 1000 * 1000; i++)
                al.Add(
new SomeValueTye(1"test"));
            sw.Stop();
            Console.WriteLine(
"AL: " + sw.Elapsed);
        }

    }


    
struct  SomeValueTye
    
{
        
public int m_TestInt;
        
public string m_TestStr;
        
public SomeValueTye(int intValue, string strValue)
        
{
            m_TestInt 
= intValue;
            m_TestStr 
= strValue;
        }

    }

 运行结果:

Constructor Time:
IL:      00:00:00.1989778
AL:     00:00:00.1317960

AddMechod Time:
IL:      00:00:00.0932551
AL:     00:00:00.2032006

 

 
1
10
100
1000
1000000
List<T>
572
212
430
782
1142039
ArrayList
164
162
215
1206
2112237

 

  IL_0020:  newobj     instance  void  IList.SomeValueTye::.ctor(int32,
                                                               
string )
  IL_0025:  callvirt   instance 
void   /* UNKNOWN TYPE (0x15)*/ ::Add( ! 0 )
  IL_0083:  newobj     instance  void  IList.SomeValueTye::.ctor(int32,
                                                               
string )
  IL_0088:  box        IList.SomeValueTye
  IL_008d:  callvirt   instance int32 [mscorlib]System.Collections.ArrayList::Add(
object )

Why?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值