C# Dictionary,List,数组 遍历性能测试 消耗时间 消耗内存

4 篇文章 0 订阅

主要是测试遍历消耗的时间和内存
测试案例

        int Num = 10000;
        int CN = 100;
        List<string> list = new List<string>();
        string[] Arr = new string[Num];
        Dictionary<int, string> dic = new Dictionary<int, string>();
        for (int i = 0; i < Num; i++)
        {
            var s = i.toString();
            list.Add(s);
            Arr[i] = s;
            dic.Add(i, s);
        }
        debug.ShowCalculate();
        System.GC.Collect();
        debug.log("dic to All Array []");
        debug.TestRunTime(() =>
        {
            string ss;
            for (int j = 0; j < CN; j++)
            {
                var Carr = new List<string>(dic.Values).ToArray();
                for (int i = 0; i < Carr.Length; i++)
                {
                    ss = Carr[i];
                }
            }
        });
        debug.log("dic to Keys list []");
        debug.TestRunTime(() =>
        {
            string ss;
            for (int j = 0; j < CN; j++)
            {
                var Carr = new List<int>(dic.Keys);
                for (int i = 0; i < Carr.Count; i++)
                {
                    ss = dic[Carr[i]];
                }
            }
        });
        debug.log("dic Enumerator");
        debug.TestRunTime(() =>
        {
            string ss;
            for (int j = 0; j < CN; j++)
            {
                var e = dic.GetEnumerator();
                while (e.MoveNext())
                {
                    ss = e.Current.Value;
                }
            }
        });
        debug.log("list []");
        debug.TestRunTime(() =>
        {
            string ss;
            for (int j = 0; j < CN; j++)
            {
               for (int i = 0; i < list.Count; i++)
                {
                    ss = list[i];
                }
            }
        });
        debug.log("Arr []");
        debug.TestRunTime(() =>
        {
            string ss;
            for (int j = 0; j < CN; j++)
                for (int i = 0; i < Arr.Length; i++)
                {
                    ss = Arr[i];
                }
        });
        System.GC.Collect();
        debug.ShowCalculate();

测试结果
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述


//测试消耗时间
public static void TestRunTime(Action LogicalSegment)
 {
		sw.Reset();
		sw.Start();
		LogicalSegment.Invoke();
		sw.Stop();
		log("运行时间:", sw.ElapsedMilliseconds, "毫秒");
 }
//测试消耗内存
public static void TestMemoryWatcher(Action action)
 {
        double start = GC.GetTotalMemory(true);
        action();
        GC.Collect();
        double end = GC.GetTotalMemory(true);
        double useMemory = (end - start) / 1024;
        log("运行内存:", useMemory,"M");
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值