我得到了我自己类的对象列表,看起来像:
public class IFFundTypeFilter_ib
{
public string FundKey { get; set; }
public string FundValue { get; set; }
public bool IsDisabled { get; set; }
}
属性IsDisabled
是通过执行查询collection.Where(some condition)
并计算匹配对象的数量来设置的。结果是不包含属性计数的IEnumarable<IFFundTypeFilter_ib>
。我想知道,什么会更快。
这个:
collection.Where(somecondition).Count();
或者这个:
collection.Where(someocondition).ToList().Count;
集合可以包含很少的对象,但也可以包含,例如 700。我将在其他条件下进行两次计数呼叫。在第一个条件下,我检查FundKey是否等于某个密钥,在第二个条件下,我做同样的事情,但我将其与其他密钥值进行比较。
你问:
我想知道,什么会更快。
每当你问你应该真正计时并找出答案时。
我开始测试所有这些获取计数的变体:
var enumerable = Enumerable.Range(0, 1000000);
var list = enumerable.ToList();
var methods = new Func<int>[]
{
() => list.Count,
() => enumerable.Count(),
() => list.Count(),
() => enumerable.ToList().Count(),
() => list.ToList().Count(),
() => enumerable.Select(x => x).Count(),
() => list.Select(x => x).Count(),
() => enumerable.Select(x => x).ToList().Count(),
() => list.Select(x => x).ToList().Count(),
() => enumerable.Where(x => x % 2 == 0).Count(),
() => list.Where(x => x % 2 == 0).Count(),
() => enumerable.Where(x => x % 2 == 0).ToList().Count(),
() => list.Where(x => x % 2 == 0).ToList().Count(),
};
我的测试代码显式运行每个方法 1,000 次,使用 Stopwatch
测量每个执行时间,并忽略发生垃圾回收的所有结果。然后,它获取每个方法的平均执行时间。
var measurements =
methods
.Select((m, i) => i)
.ToDictionary(i => i, i => new List<double>());
for (var run = 0; run < 1000; run++)
{
for (var i = 0; i < methods.Length; i++)
{
var sw = Stopwatch.StartNew();
var gccc0 = GC.CollectionCount(0);
var r = methods[i]();
var gccc1 = GC.CollectionCount(0);
sw.Stop();
if (gccc1 == gccc0)
{
measurements[i].Add(sw.Elapsed.TotalMilliseconds);
}
}
}
var results =
measurements
.Select(x => new
{
index = x.Key,
count = x.Value.Count(),
average = x.Value.Average().ToString("0.000")
});
以下是结果(从最慢到最快排序(:
+---------+-----------------------------------------------------------+
| average | method |
+---------+-----------------------------------------------------------+
| 14.879 | () => enumerable.Select(x => x).ToList().Count(), |
| 14.188 | () => list.Select(x => x).ToList().Count(), |
| 10.849 | () => enumerable.Where(x => x % 2 == 0).ToList().Count(), |
| 10.080 | () => enumerable.ToList().Count(), |
| 9.562 | () => enumerable.Select(x => x).Count(), |
| 8.799 | () => list.Where(x => x % 2 == 0).ToList().Count(), |
| 8.350 | () => enumerable.Where(x => x % 2 == 0).Count(), |
| 8.046 | () => list.Select(x => x).Count(), |
| 5.910 | () => list.Where(x => x % 2 == 0).Count(), |
| 4.085 | () => enumerable.Count(), |
| 1.133 | () => list.ToList().Count(), |
| 0.000 | () => list.Count, |
| 0.000 | () => list.Count(), |
+---------+-----------------------------------------------------------+
这里有两件事很重要。
首先,任何具有内联.ToList()
的方法都比没有内联的等效方法慢得多。
第二,LINQ 运算符在可能的情况下利用可枚举的基础类型来快捷方式计算。enumerable.Count()
和list.Count()
方法表明了这一点。
list.Count
调用和list.Count()
调用之间没有区别。因此,关键的比较是enumerable.Where(x => x % 2 == 0).Count()
和enumerable.Where(x => x % 2 == 0).ToList().Count()
调用之间的比较。由于后者包含一个额外的操作,我们预计它需要更长的时间。它几乎长了 2.5 毫秒。
我不知道你为什么说你要调用计数代码两次,但如果你这样做,最好建立列表。如果不是,只需在查询后进行普通.Count()
调用。
通常,具体化到列表的效率会降低。
此外,如果使用两个条件,则缓存结果或将查询具体化为List
是没有意义的。
您应该只使用接受谓词的 Count
重载:
collection.Count(someocondition);
正如@CodeCaster在评论中提到的,它相当于collection.Where(condition).Count()
,但更具可读性和简洁性。
正是这样使用它
var count = collection.Where(somecondition).ToList().Count;
没有意义 - 填充列表只是为了获取计数,因此在这种情况下使用 IEnumerable<T>.Count()
是合适的方法。
在您做这样的事情的情况下,使用 ToList
是有意义的
var list = collection.Where(somecondition).ToList();
var count = list.Count;
// do something else with the list
没有ToList()
的纯Count()
背后的 SQL 非常直接,不会强制任何填充列表所需的数据传输。