如何衡量IEnumerable <t>评估时间?</t>

时间:2012-05-02 10:40:11

标签: c# linq ienumerable

我有一个懒惰的linq查询,它可以发现内存中某些包含特定属性的xml中的元素数量。我想评估为不同长度的xml枚举该查询所需的时间长度。

为了做到这一点,我首先考虑使用.ToList()调用,因为我知道这会导致所有Items的枚举。但是我决定这可能不代表完整的评估时间,因为会有一个内存操作将所有项目从一个结构移动到另一个结构,因此切换到.Count()的调用。

IEnumerable<XMLNode> nodes = "<Some Linq>"

var watch = new Stopwatch();
watch.Restart();
var test = nodes.Count();
watch.Stop();
Console.WriteLine("Enumeration Time: " + watch.ElapsedMilliseconds);

第一个问题是这是计算评估可枚举所需时间的最佳方法吗?

第二个问题是哪个更能代表实际评估时间Count()或ToList()?

以下是代码的一些结果,请注意,在迭代之间添加了一小部分xml,这意味着每次搜索的xml都会增加相同的数量。

Resuts(来自.Count())

Enumeration Time: 0 (ms)
Enumeration Time: 0 (ms)
Enumeration Time: 1 (ms)
Enumeration Time: 1 (ms)
Enumeration Time: 2 (ms)
Enumeration Time: 2 (ms)
Enumeration Time: 2 (ms)
Enumeration Time: 3 (ms)
Enumeration Time: 3 (ms)
Enumeration Time: 4 (ms)
Enumeration Time: 4 (ms)
Enumeration Time: 5 (ms)
Enumeration Time: 6 (ms)
Enumeration Time: 6 (ms)
Enumeration Time: 8 (ms)
Enumeration Time: 6 (ms)
Enumeration Time: 15 (ms)
Enumeration Time: 8 (ms)
Enumeration Time: 8 (ms)
Enumeration Time: 9 (ms)
Enumeration Time: 8 (ms)
Enumeration Time: 9 (ms)
Enumeration Time: 10 (ms)
Enumeration Time: 10 (ms)
Enumeration Time: 10 (ms)
Enumeration Time: 27 (ms)
Enumeration Time: 12 (ms)
Enumeration Time: 18 (ms)
Enumeration Time: 20 (ms)

Resuts(来自.ToList())

Enumeration Time: 1 (ms)
Enumeration Time: 1 (ms)
Enumeration Time: 1 (ms)
Enumeration Time: 2 (ms)
Enumeration Time: 2 (ms)
Enumeration Time: 3 (ms)
Enumeration Time: 3 (ms)
Enumeration Time: 4 (ms)
Enumeration Time: 4 (ms)
Enumeration Time: 5 (ms)
Enumeration Time: 5 (ms)
Enumeration Time: 9 (ms)
Enumeration Time: 14 (ms)
Enumeration Time: 12 (ms)
Enumeration Time: 10 (ms)
Enumeration Time: 8 (ms)
Enumeration Time: 9 (ms)
Enumeration Time: 10 (ms)
Enumeration Time: 13 (ms)
Enumeration Time: 12 (ms)
Enumeration Time: 12 (ms)
Enumeration Time: 16 (ms)
Enumeration Time: 21 (ms)
Enumeration Time: 18 (ms)
Enumeration Time: 15 (ms)
Enumeration Time: 15 (ms)
Enumeration Time: 23 (ms)
Enumeration Time: 15 (ms)
Enumeration Time: 38 (ms)

2 个答案:

答案 0 :(得分:3)

使用var test = nodes.Count();的问题是底层集合(如果有的话)可能正在实现具有IList<T>属性的Count

作为一种优化,可以调用属性,无论集合大小,你都会接近恒定时间。

而不是ToList() Count()考虑实际迭代:

foreach(var item in nodes){}

请注意,我们没有对item做任何事情 - 这只会以最小的开销进行迭代。

答案 1 :(得分:1)

对于从文件中读取,你无法真正做出有用的性能测试。

根据文件是否在磁盘缓存中,性能会有很大差异。在您的测试中,文件将始终在您几毫秒前访问它时进行缓存,但在实际情况下它很少会出现,这会产生完全不同的结果。

您只能通过从MemoryStream而不是真实文件中读取来测试解析文件数据的部分。但是,读取实际文件通常比解析它要花费更长的时间,因此解析的性能通常并不重要。