c#之循环效率

本文通过构建不同数据类型的数组和集合,并采用多种遍历方法(如for、foreach及委托等方式),比较了它们在处理1万和5万条数据时的效率。实测结果有助于开发者选择最适合项目的遍历方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很多人在保存数据时候对于使用数组、集合等?然后遍历数据的时候是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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值