Hashtable 速度 Test Compare

第一步 ,新建测试的接口和类 。

    public interface ITest
    {
        void M();
    }
    public  class Test1:ITest
    {
        public void M()
        {
       
        }
    }

    class Test
    {
        public Test()
        {
        }
    }

第二步,分别比较 int,class,Interface在hashtable的速度

      class Program
    {
        static void Main(string[] args)
        {
            int CompareCount = 100000;
            Hashtable table1 = new Hashtable();
            System.Diagnostics.Stopwatch stopWatch1 = new System.Diagnostics.Stopwatch();
            stopWatch1.Start();
            for (int i = 0; i < CompareCount; i++)
            {
                table1.Add(i, i);
            }
            stopWatch1.Stop();

            for (int i = 0; i < CompareCount; i++)
            {
                int o = (int)table1[i];
            }

            string t2 = stopWatch1.ElapsedTicks.ToString();


            Console.WriteLine("int:" + t2);

            Hashtable table = new Hashtable();
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start();

            for (int i = 0; i < CompareCount; i++)
            {
                table.Add(i, new Test());

               
            }

            stopwatch.Stop();

            for (int i = 0; i < CompareCount; i++)
            {

                Test o = table[i] as Test;
            }
            string t1 = stopwatch.ElapsedTicks.ToString();
            Console.WriteLine("class:"+ t1);


           Hashtable table2 = new Hashtable();
           System.Diagnostics.Stopwatch stopWatch2 = new System.Diagnostics.Stopwatch();
           stopWatch2.Start();
           for (int i = 0; i < CompareCount; i++)
           {
               ITest test2 = new Test1();
               table2.Add(i,test2);
           }

           stopWatch2.Stop();
           for (int i = 0; i < CompareCount; i++)
           {
               ITest o = table2[i] as ITest;
           }
           string t3 = stopWatch2.ElapsedTicks.ToString();
           Console.WriteLine("Interface:"+t3);
            Console.Read();
        }
    }


比较结果如下:

CompareCount = 100000;


Debug=1
int:27037
class:38347
Interface:134340

Debug=2
int:27654
class:37086
Interface:134569

Run=1
int:29618
class:41159
Interface:152794

Run=2
int:27703
class:40221
Interface:155381


注意: interface 在千万级时异常  Unhandled Exception: OutOfMemoryException.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑头人

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值