LINQ 与 foreach 迭代器块的 C# 性能 [英] C# Performance of LINQ vs. foreach iterator block

查看:25
本文介绍了LINQ 与 foreach 迭代器块的 C# 性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1) 这些生成相同的字节码吗?

1) Do these generate the same byte code?

2) 如果不是,在某些情况下使用一个比另一个有什么好处吗?

2) If not, is there any gain in using one over the other in certain circumstances?

// LINQ select statement
return from item in collection
    select item.Property;

// foreach in an iterator block
foreach (item in collection)
    yield return item.Property;

推荐答案

  1. 它们不会生成相同的代码,而是归结为相同的东西,您会得到一个实现 IEnumerable 的对象.不同之处在于 linq 从其库中提供迭代器(在本例中最有可能使用 WhereSelectArrayIteratorWhereSelectListIterator),而在第二个示例中,您自己生成了一个迭代器块,用于剖析收藏.迭代器块方法总是通过编译器魔法被编译为实现 IEnumerable 的单独类,您看不到它,但在调用迭代器块方法时隐式实例化.

  1. They don't generate the same code but boil down to the same thing, you get an object implementing IEnumerable<typeof(Property)>. The difference is that linq provides iterator from its library (in this case most likely by using WhereSelectArrayIterator or WhereSelectListIterator) whereas in the second example you yourself generate an iterator block that dissects a collection. An iterator block method is always, by ways of compiler magic, compiled as a separate class implementing IEnumerable<typeof(yield)> which you don't see but instantiate implicitly when you call iterator block method.

性能方面,#1 对于可索引集合应该稍微(但只是稍微)快一点,因为当您遍历结果 IEnumerable 时,您会直接从 foreach 进入优化的集合检索linq 迭代器.在示例#2 中,您从 foreach 进入迭代器块的 foreach 并从那里进入集合检索,您的性能主要取决于编译器在优化产量逻辑方面的智能程度.无论如何,我认为对于任何复杂的收集机制,检索成本都会使这种差异边缘化.

Performance wise, #1 should be slightly (but just slightly) faster for indexable collections because when you loop through the resulting IEnumerable you go directly from your foreach into collection retrieval in an optimized linq iterator. In example #2 you go from foreach into your iterator block's foreach and from there into collection retrieval and your performance depends mostly on how smart the compiler is at optimizing yield logic. In any case I would imagine that for any complex collection mechanism the cost of retrieval marginalizes this difference.

恕我直言,我总是选择#1,如果没有别的办法可以让我不必为迭代编写单独的方法.

IMHO, I would always go with #1, if nothing else it saves me from having to write a separate method just for iterating.

这篇关于LINQ 与 foreach 迭代器块的 C# 性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆