Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 并行。用于使用步骤!=1._C#_For Loop_Parallel Processing_Task Parallel Library - Fatal编程技术网

C# 并行。用于使用步骤!=1.

C# 并行。用于使用步骤!=1.,c#,for-loop,parallel-processing,task-parallel-library,C#,For Loop,Parallel Processing,Task Parallel Library,是否有任何方法可以实现此For循环的并行版本 for (int i = 0; i < 100; i += 2) { DoStuff(i); } 这里有一个提示: for (int j = 0; j < 50; j++) { i = 2*j; DoStuff(); } for(int j=0;jDoStuff(索引)); } 专用静态IEnumerable步进数据采集器(int startIndex、int endIndex、int stepSize) { 对于(int i=sta

是否有任何方法可以实现此
For
循环的
并行版本

for (int i = 0; i < 100; i += 2) { DoStuff(i); }
这里有一个提示:

for (int j = 0; j < 50; j++) { i = 2*j; DoStuff(); }
for(int j=0;j<50;j++){i=2*j;DoStuff();}

一般来说,看看你是否能计算出迭代次数以及从迭代次数到变量值的转换。

Ben的建议非常适合于常数步,例如+2、+3等

或者(如果您的步骤是随机的),您可以使用
Parallel.ForEach

int[] input = { 1, 3, 4, 5, 7, 10, 20, 25 }; 

Parallel.ForEach(input,
    () => new Bar(), //thread local data initialize
    (i, state, local) => //loop body
    {
        // your code
    },
    (local) => //thread local data post-action
    {
        // your code
    }
变量
i
将从
input
数组中获取数据。您可以将
输入
替换为
可枚举的.Range
(或将其与
等组合使用)


如果您只想在
i
变量中获取素数,那么这将非常有效

这里是处理分级索引的另一种方法

private void ParallelForEachProcessSteppedIndexes()
        {
            Parallel.ForEach(SteppedIterator(0, 100, 2), (index) => DoStuff(index));
        }

private static IEnumerable<int> SteppedIterator(int startIndex, int endIndex, int stepSize)
        {
            for (int i = startIndex; i < endIndex; i = i + stepSize)
            {
                yield return i;
            }
        }
private void parallelForEachProcessSteppedIndex()的
{
Parallel.ForEach(步进检测器(01100,2),(索引)=>DoStuff(索引));
}
专用静态IEnumerable步进数据采集器(int startIndex、int endIndex、int stepSize)
{
对于(int i=startIndex;i
在转换到VB.NET的新迭代器函数后,Toan的答案对我来说很有用

Private Sub LoopExample()
    Parallel.ForEach(SteppedIterator(1,100,5), AddressOf Test)

End Sub

Private Iterator Function SteppedIterator(startIndex As Integer, endIndex As Integer, stepSize As Integer) As IEnumerable(Of Integer)
    For i As Integer = startIndex To endIndex Step stepSize
        Yield i
    Next

End Function

Private Sub Test(i As Integer, state As ParallelLoopState, index As Long)
    Debug.WriteLine(i.ToString)
End Sub

没有什么比一个简单得可笑的解决方案更能让我觉得自己愚蠢了:)谢谢。我不知道为什么我以前错过了
Parallel的线程局部重载。对于ForEach
,我认为线程局部性只在
Parallel.For
中可用。谢谢
Private Sub LoopExample()
    Parallel.ForEach(SteppedIterator(1,100,5), AddressOf Test)

End Sub

Private Iterator Function SteppedIterator(startIndex As Integer, endIndex As Integer, stepSize As Integer) As IEnumerable(Of Integer)
    For i As Integer = startIndex To endIndex Step stepSize
        Yield i
    Next

End Function

Private Sub Test(i As Integer, state As ParallelLoopState, index As Long)
    Debug.WriteLine(i.ToString)
End Sub