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万数据

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

原文地址:https://www.cnblogs.com/kq123321/p/6489242.html