When to use which for?(什么时候用哪个?)
问题描述
编辑下面的附加选项和稍微扩展的问题.
EDIT Additional options and a slightly extended question below.
考虑这个类体的人为的抽象示例.它演示了执行for"迭代的四种不同方式.
Consider this contrived and abstract example of a class body. It demonstrates four different ways of performing a "for" iteration.
private abstract class SomeClass
{
public void someAction();
}
void Examples()
{
List<SomeClass> someList = new List<SomeClass>();
//A. for
for (int i = 0; i < someList.Count(); i++)
{
someList[i].someAction();
}
//B. foreach
foreach (SomeClass o in someList)
{
o.someAction();
}
//C. foreach extension
someList.ForEach(o => o.someAction());
//D. plinq
someList.AsParallel().ForAll(o => o.someAction());
从答案和研究中添加一些选项.
Addition of some options from answers and research.
//E. ParallelEnumerable
ParallelEnumerable.Range(0, someList.Count - 1)
.ForAll(i => someList[i].someAction());
//F. ForEach Parallel Extension
Parallel.ForEach(someList, o => o.someAction());
//G. For Parallel Extension
Parallel.For(0, someList.Count - 1, i => someList[i].someAction())
}
我的问题分为两部分.我错过了一些重要的选择吗?考虑到可读性但主要是性能,哪个选项是最佳选择?
My question comes in two parts. Have I missed some significant option? Which option is the best choice, considering readability but primarily performance?
请说明 SomeClass
实现的复杂性或 someList
的 Count
是否会影响此选择.
Please indicate if the complexity of the SomeClass
implementation, or the Count
of someList
would effect this choice.
有了这么多令人眼花缭乱的选项,我不希望我的代码被选择破坏.要为我的问题添加第三部分,如果我的列表可以是任意长度,我应该默认为并行选项吗?
With such a dizzying array of options, I wouldn't like my code to be spoilt by choice. To add a thrid part to my question, If my list could be any length should I default to a parallel option?
作为一个稻草人.我怀疑 SomeClass
的所有实现和 someList
选项 //E 的所有长度.考虑到多处理器架构的普遍性,ParallelEnumerable
将提供最佳的平均性能.我没有做任何测试来证明这一点.
As a straw man. I suspect that over all implementations of SomeClass
and all lengths of someList
option //E. ParallelEnumerable
would offer the best average performance, given the prevalanece of multi processor architechtures. I haven't done any testing to prove this.
注意:并行扩展需要使用 System.Threading.Tasks
命名空间.
Note: The parallel extensions will require the use of the System.Threading.Tasks
namespace.
推荐答案
选项 A 仅对实现索引的序列才真正有意义,并且仅对具有 O(1)
查找时间的序列有效.一般来说,我会使用 foreach
和变体,除非你有特殊的逻辑.
Option A only really makes sense for sequences that implement indexing and will only be performant for those that have O(1)
lookup time. Generally, I would use the foreach
and variants unless you have special logic.
还要注意,像 for (int i = 1; i
foreach(var item按顺序跳过(1))
.
Also note, that "special logic" like for (int i = 1; i < list.Count; i++)
can be implemented with Linq extension methods: foreach(var item in sequence.Skip(1))
.
因此,通常更喜欢 B 而不是 A.
So, generally prefer B over A.
至于 C:如果其他开发人员不习惯函数式风格,这可能会让他们感到困惑.
As to C: This can be confusing for other developers if they aren't used to the functional style.
至于 D:这取决于很多因素.我想对于简单的计算,你不想这样做 - 如果循环体需要一段时间来计算,你只会真正从并行化中受益.
As to D: This will depend on a lot of factors. I guess for simple calculations, you don't want to do this - you will only really benefit from parallelization if the loop body takes a while to compute.
这篇关于什么时候用哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么时候用哪个?
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30