很多人在保存数据时候对于使用数组、集合等?然后遍历数据的时候是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 }; } }
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(); }
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(); }
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(); }
1万数据
5万数据
没有对比就没有伤害,所以大家在项目中遇到这类问题,还是选择合适自己的方法进行......
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?