How can I maximize the performance of element-wise operation on an big array in C#

自古美人都是妖i 提交于 2019-12-04 08:18:32

The problem is that while invoking a delegate is relatively fast, it adds up when you invoke it many times and the code inside the delegate is very simple.

What you could try instead is to use a Partitioner to specify the range you want to iterate, which allows you to iterate over many items for each delegate invocation (similar to what you're doing in [1]):

Parallel.ForEach(Partitioner.Create(0, n * m), partition =>
    {
        for (int i = partition.Item1; i < partition.Item2; i++)
        {
            W[i] *= C[i];
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!