zoukankan      html  css  js  c++  java
  • c#之循环效率

    很多人在保存数据时候对于使用数组、集合等?然后遍历数据的时候是for、froeach?

    下面我就写一个小例子进行测试看看,话不多说,直接用数据说话。

    1.构建数据分别是数组、集合构建,数据类型分别是值类型、引用类型

    public static List<int> ListData = new List<int>();
            public static int[] ArrayData = new int[10000];
            public static List<people> ListPople = new List<people>();
            public static people[] ArrayPeople = new people[10000];
     public class people{
            public string Name { get; set; }
            public int Age { get;set }
            public int Sex { get; set; }
        }
    public static void InitData() {
    
                for (int i = 0, j = 10000; i < j; i++)
                {
                    ListData.Add(i);
                    ArrayData[i] = i;
                    ListPople.Add(new people() { Age=i, Name="N"+i, Sex=1 });
                    ArrayPeople[i] = new people() { Age = i, Name = "N" + i, Sex = 1 };
                }
            }
    View Code

    2.for

    static void Main(string[] args)
            {
                System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
                Console.WriteLine("值类型for (int i = 0; i < count; i++)");
                stopwatch.Start();  
                for (int i = 0; i < ArrayData.Length; i++)
                {
                    Console.WriteLine(ArrayData[i]);
                }
                stopwatch.Stop();
                Console.WriteLine("值类型使用时间:"+ stopwatch.ElapsedTicks);
                stopwatch.Reset();
    
                Console.WriteLine("值类型for (int i = 0,j =count;i<j;i++)");
                stopwatch.Start();
                for (int i = 0,j = ArrayData.Length; i < j; i++)
                {
                    Console.WriteLine(ArrayData[i]);
                }
                stopwatch.Stop();
                Console.WriteLine("使用时间:" + stopwatch.ElapsedTicks);
                stopwatch.Reset();
    
    
                Console.WriteLine("引用类型 for (int i = 0; i < count; i++)");
                stopwatch.Start();
                for (int i = 0; i < ArrayPeople.Length; i++)
                {
                    Console.WriteLine(ArrayPeople[i].Name);
                }
                stopwatch.Stop();
                Console.WriteLine("使用时间:" + stopwatch.ElapsedTicks);
                stopwatch.Reset();
    
                Console.WriteLine("值类型for (int i = 0,j =count;i<j;i++)");
                stopwatch.Start();
                for (int i = 0, j = ArrayPeople.Length; i < j; i++)
                {
                    Console.WriteLine(ArrayPeople[i].Name);
                }
                stopwatch.Stop();
                Console.WriteLine("使用时间:" + stopwatch.ElapsedTicks);
                stopwatch.Reset();
    
    
                Console.ReadLine();
    
            }
    View Code

    1万数据

    5万数据

    3.foreach

    static void Main(string[] args)
            {
                InitData();
    
                System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
                Console.WriteLine("值类型 foreach");
                stopwatch.Start();
                foreach (int i in ListData)
                {
                    //Console.WriteLine(i);
                }
                stopwatch.Stop();
                Console.WriteLine("值类型使用时间:" + stopwatch.ElapsedTicks);
                stopwatch.Reset();
    
    
                Console.WriteLine("引用类型 foreach");
                stopwatch.Start();
                foreach (var i in ListPople)
                {
                    //Console.WriteLine(i.Name);
                }
                stopwatch.Stop();
                Console.WriteLine("使用时间:" + stopwatch.ElapsedTicks);
                stopwatch.Reset();
    
    
                Console.ReadLine();
    
            }
    View Code

    1万数据

    5万数据

    4.委托货其他方式

    static void Main(string[] args)
            {
                InitData();
    
                System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
                Console.WriteLine("数值类型:Array.ForEach");
                stopwatch.Start();
                Array.ForEach<int>(ListData.ToArray(), (value)=>{
                });
                stopwatch.Stop();
                Console.WriteLine("值类型使用时间:" + stopwatch.ElapsedTicks);
                stopwatch.Reset();
    
                Console.WriteLine("引用类型:Array.ForEach");
                stopwatch.Start();
                Array.ForEach<people>(ListPople.ToArray(), (value) => {
                });
                stopwatch.Stop();
                Console.WriteLine("值类型使用时间:" + stopwatch.ElapsedTicks);
                stopwatch.Reset();
    
    
                Console.WriteLine("数值类型:Parallel.ForEach");
                stopwatch.Start();
                Parallel.ForEach(ListData, (value) => {});
                stopwatch.Stop();
                Console.WriteLine("使用时间:" + stopwatch.ElapsedTicks);
                stopwatch.Reset();
    
                Console.WriteLine("引用类型:Parallel.ForEach");
                stopwatch.Start();
                Parallel.ForEach(ListPople, (value) => { });
                stopwatch.Stop();
                Console.WriteLine("使用时间:" + stopwatch.ElapsedTicks);
                stopwatch.Reset();
    
                Console.ReadLine();
    
            }
    View Code

    1万数据

    5万数据

    没有对比就没有伤害,所以大家在项目中遇到这类问题,还是选择合适自己的方法进行......

  • 相关阅读:
    详细版Jmeter随机参数的接口并发测试总结
    Windows下MQTT代理服务器的搭建
    关于使用elascticsearch的两个小技巧
    解决easyswoole的swServer_start_check: onTask event callback must be set at报错
    解决使用宝塔安装的swoole扩展,运行项目出现的3个常见问题
    浅谈一下ThinkPHP5.1实现事务嵌套的特性
    资源出现多个 "Access-Control-Allow-Origin"
    Mac 制作系统启动盘
    深入剖析分布式一致性共识算法
    分布式系统限流算法分析与实现
  • 原文地址:https://www.cnblogs.com/kq123321/p/6489242.html
Copyright © 2011-2022 走看看