C# foreach,linq,delegate集合查询的性能比较
2011-11-04 11:11
375 查看
因为前面,叶鹏(http://www.cnblogs.com/jqbird/archive/2011/11/04/2235627.html) 已经做过测试,我只是重新验证了一下,发现他的结果是不对的。我得出的结果是,foreach > delegate > linq,这个是我反复测试的结果
代码和他的差不多
public class TestClass
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
}
static void Main(string[] args)
{
test();
}
private static void test()
{
IList<TestClass> list1 = new List<TestClass>();
for (int i = 0; i < 10000000; i++)
{
TestClass tc = new TestClass();
tc.Id = i;
tc.Name = "Test Data" + i;
list1.Add(tc);
}
Stopwatch timer = new Stopwatch();
#region for circle
timer.Start();
int count = 0;
foreach (TestClass tc in list1)
{
if (1 <= tc.Id && tc.Id < 1000)
{
count++;
}
}
timer.Stop();
Console.Write("Count:" + count + ", for circle time:");
Console.WriteLine(timer.Elapsed.Ticks);
#endregion
#region linq
timer = new Stopwatch();
timer.Start();
int count2 = 0;
count2 = list1.Where(p => p.Id >= 1 && p.Id < 1000).Count();
timer.Stop();
Console.Write("Count:" + count2 + ", linq time:");
Console.WriteLine(timer.Elapsed.Ticks);
#endregion
#region delegate
timer = new Stopwatch();
timer.Start();
int count3 = list1.ToList().FindAll(delegate(TestClass post)
{
return post.Id >= 1 && post.Id < 1000;
}).Count;
timer.Stop();
Console.Write("Count:" + count3 + ", delegate time:");
Console.WriteLine(timer.Elapsed.Ticks);
#endregion
Console.Read();
}
而我得出的结论:
希望广大朋友指正,水平有限!!
代码和他的差不多
public class TestClass
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
}
static void Main(string[] args)
{
test();
}
private static void test()
{
IList<TestClass> list1 = new List<TestClass>();
for (int i = 0; i < 10000000; i++)
{
TestClass tc = new TestClass();
tc.Id = i;
tc.Name = "Test Data" + i;
list1.Add(tc);
}
Stopwatch timer = new Stopwatch();
#region for circle
timer.Start();
int count = 0;
foreach (TestClass tc in list1)
{
if (1 <= tc.Id && tc.Id < 1000)
{
count++;
}
}
timer.Stop();
Console.Write("Count:" + count + ", for circle time:");
Console.WriteLine(timer.Elapsed.Ticks);
#endregion
#region linq
timer = new Stopwatch();
timer.Start();
int count2 = 0;
count2 = list1.Where(p => p.Id >= 1 && p.Id < 1000).Count();
timer.Stop();
Console.Write("Count:" + count2 + ", linq time:");
Console.WriteLine(timer.Elapsed.Ticks);
#endregion
#region delegate
timer = new Stopwatch();
timer.Start();
int count3 = list1.ToList().FindAll(delegate(TestClass post)
{
return post.Id >= 1 && post.Id < 1000;
}).Count;
timer.Stop();
Console.Write("Count:" + count3 + ", delegate time:");
Console.WriteLine(timer.Elapsed.Ticks);
#endregion
Console.Read();
}
而我得出的结论:
希望广大朋友指正,水平有限!!
相关文章推荐
- C#中 foreach,linq,delegate集合查询的性能比较
- C# foreach,linq,delegate集合查询的性能比较
- 一起谈.NET技术,三种属性操作性能比较:PropertyInfo + Expression Tree + Delegate.CreateDelegate
- 用LINQ在集合中查询特定对象
- AdoNet vs LinqToSql vs NIntegrateQuery查询性能测试
- atitit. 集合groupby 的实现(2)---自定义linq查询--java .net php
- Oracle 性能测试一:嵌套SQL的查询速度比较分析(初级)
- 数组排序方法的性能比较(3):LINQ排序实现分析
- 关于 去掉重复的面试题 涉及rowid 和id 的性能比较 外表字段和子查询字段的连接
- DataRabbit 轻量的数据访问框架(14)-- DataRabbit 3.0 与 Linq to sql 性能比较
- SQL查询性能分析之(not in)、(and not)、(<>)、(!=)性能比较
- 【C#.NET】C#六种集合性能比较
- 三种属性操作性能比较:PropertyInfo + Expression Tree + Delegate.CreateDelegate
- atitit. 集合groupby 的实现(2)---自定义linq查询--java .net php
- DataRabbit 3.0 与 Linq to sql 性能比较
- 学习用,Linq中的Lambda语法和Linq查询语法比较,欢迎大家来看,嘿嘿,每个人心中也有自己的想法。
- C#六种集合性能比较
- C# 泛型集合List和非泛型集合ArrayList的性能比较
- LINQ(语言集合查询)疑问和理解
- 线程安全的List集合(性能比较差..)