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

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

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

public static List<int> ListData = new List<int>();
public static int[] ArrayData = new int[];
public static List<people> ListPople = new List<people>();
public static people[] ArrayPeople = new people[];
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 = , j = ; i < j; i++)
{
ListData.Add(i);
ArrayData[i] = i;
ListPople.Add(new people() { Age=i, Name="N"+i, Sex= });
ArrayPeople[i] = new people() { Age = i, Name = "N" + i, Sex = };
}
}

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 = ; 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 = ,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 = ; 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 = , 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万数据

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

c#之循环效率的更多相关文章

  1. for、foreach和MoveNext循环效率粗比较

    今天没事对for循环.foreach循环.MoveNext循环,执行效率进行了对比:粗略测试代码如下: static void Main(string[] args) { #region 三种方式循环 ...

  2. foreach与正常for循环效率对比

    foreach foreach编译成字节码之后,使用的是迭代器实现的. foreach特点: 无须获取容器大小 需要创建额外的迭代器变量 遍历期间得到的是对象,没有索引位置信息,因此不能进行赋值操作. ...

  3. ArrayList哪种循环效率更好你真的清楚吗

    ArrayList简介 声明:以下内容都是基于jdk1.8的 ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了 ...

  4. 循环效率对比 js node c# mssql

  5. C++ for循环效率

    1.考虑二维数组,在C++中,以先行后列的方式存储连续的数组元素.也就是同一行的元素在一起,同一列的元素之间有间隔,且间隔相同.理想情况下,二维数组的元素是随机访问的,可以直接定位,即i*列数+j.因 ...

  6. 两个for循环效率,哪个高

    在多重循环中,如果有可能,应当将最长的循环放在最内层,最短的循环放在最外层,以减少CPU跨切循环层的次数.

  7. Oracle游标进行循环效率比较

    对300万一张表数据,用游标进行循环,不同写法的效率比较 对300万一张表数据,用游标进行循环,不同写法的效率比较   1.显示游标   declare     cursor cur_2 is sel ...

  8. Java中for each与正常for循环效率对比

    循环ArrayList时,普通for循环比foreach循环花费的时间要少一点:循环LinkList时,普通for循环比foreach循环花费的时间要多很多. 当我将循环次数提升到一百万次的时候,循环 ...

  9. 对一致性Hash算法,Java代码实现的深入研究

    一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性 ...

随机推荐

  1. iOS + UIWebView 实践

    1. 调用java script 现在只能实现弹出窗口 [self.m_webView stringByEvaluatingJavaScriptFromString:@"alert(1)&q ...

  2. python查询mysql中文乱码问题

    python2.7 查询或者插入中文数据在mysql中的时候出现中文乱码 --- 可能情况: 1.mysql数据库各项没有设置编码,默认为'latin' 2.使用MySQL.connect的时候没有设 ...

  3. Object.create() 实现

    if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototyp ...

  4. IOS 利用图片设置背景

    UIImageView* imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; imageView.image = [UI ...

  5. 笔记整理——C语言-http-1

    http 传输原理及格式 - friping - ITeye技术网站 - Google Chrome (2013/4/1 14:02:36) http 传输原理及格式 博客分类: 其他 应用服务器浏览 ...

  6. 利用 Grunt (几乎)无痛地做前端开发 (一)之单元测试

    前言 如果你想开发一个js应用,甭管多简单,都要先创建整个宇宙 来看看我们的Javascript小宇宙: 确定如何根据需求.功能划分模块,如何将代码分成多个文件开发,合成一个发布 保证上一条的同时,使 ...

  7. 1.3.2. App Icon 和 Launch Image(Core Data 应用程序实践指南)

    App Icon: 选中 Assets.xcassets 选择 AppIcon ,并拖入图片(29.40.60) Launch Image: 创建 Launch Image 拖入图片(2x.R4)

  8. CSS 文本、字体、链接

    CSS 文本属性可定义文本的外观. 通过文本属性,您可以改变文本的颜色.字符间距,对齐文本,装饰文本,对文本进行缩进,等等. 缩进文本 把 Web 页面上的段落的第一行缩进,这是一种最常用的文本格式化 ...

  9. 10款面向HTML5 画布(Canvas)的JavaScript库

    https://www-evget-com/article/2014/4/9/20799.html

  10. 使用 GitHub, Jekyll 打造自己的免费独立博客

    使用 GitHub, Jekyll 打造自己的免费独立博客 GitHub是一个代码托管网站,现在很多开源项目都放在GitHub上. 利用GitHub,可以让全球各地的程序员们一起协作开发.GitHub ...