C#性能3中的并行For循环 [英] Parallel For loops in C# performance 3

查看:123
本文介绍了C#性能3中的并行For循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能给我看一个获取列表的例子,使用并行for循环对列表'属性中的每个元素进行一些操作然后返回原始列表吗?例如

Can anyone show me an example of taking a list, doing some operation in each element in the list''s property using a parallel for loop and then returning the original list? For example

List<t> someList = new List<t>
for(int i= 0; i < originallist.count;i++)
{
    Do operation on originallist[i].somevariable
    someList.add(originallist[i])
}
return someList





换句话说。我将如何并行化这个???



In other words. HOW would I parallelize this???

推荐答案

我建​​议阅读这篇文章任务并行库:n中的3个 [ ^ ]。另请注意,整个系列读起来非常好。
I suggest going through this article Task Parallel Library: 3 of n[^]. Also note that the whole serie is a really good read.






请参考以下文件:如何:编写一个简单的Parallel.For循环 [ ^ ]



这是代码段:



Hi,

Please refer to the following document: How to: Write a Simple Parallel.For Loop[^]

Here is a code snippet:

static void MultiplyMatricesParallel(double[,] matA, double[,] matB, double[,] result)
{
    int matACols = matA.GetLength(1);
    int matBCols = matB.GetLength(1);
    int matARows = matA.GetLength(0);

    // A basic matrix multiplication.
    // Parallelize the outer loop to partition the source array by rows.
    Parallel.For(0, matARows, i =&gt;
    {
        for (int j = 0; j &lt; matBCols; j++)
        {
            // Use a temporary to improve parallel performance.
            double temp = 0;
            for (int k = 0; k &lt; matACols; k++)
            {
                temp += matA[i, k] * matB[k, j];
            }
            result[i, j] = temp;
        }
    }); // Parallel.For
}

< br $> b $ b

亲切的问候,



Kind regards,


这篇关于C#性能3中的并行For循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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