随笔 - 435  文章 - 0  评论 - 111  阅读 - 62万 

对内存中的数据做并行运算,

AsParallel(并行化)

就是在集合后加个AsParallel()

性能比较: 并行用了27秒,不用并行用了33秒

1
2
3
4
5
6
7
8
9
10
11
12
var Elements = EleList.Where(m =>
                                        {
                                            foreach (var rule in tmpRules)
                                            {
                                                rule.Status = false;
                                                if (!rule.Match(m))
                                                {
                                                    break;
                                                }
                                            }
                                            return tmpRules.Where(r => r.Status == false).Count() == 0;
                                        }).AsParallel().ToList();

 使用QueueUserWorkItem,速度又有所提升,大概10秒左右 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var doneEvent = new ManualResetEvent(false);
int taskCount = 0;
 
for (int i = 0; i < filteredEleList.Count; i++)
{
    int index = i;
    Interlocked.Increment(ref taskCount);
    matchTmpRules[index] = tmpRules;
 
    ThreadPool.QueueUserWorkItem((object evt) =>
    {
        try
        {
            var field = filteredEleList[index];
             
            foreach (var rule in matchTmpRules[index])
            {
                rule.Status = false;
                if (!rule.Match(field))
                {
                    break;
                }
            }
 
            if(tmpRules.Where(r => r.Status == false).Count() == 0)
            {
                tmpElements.Add(field);
            }
 
        }
        catch (Exception ex)
        {
            //Logger.Write(ex, "There was an error while trying to match field '" + fields[index].Name + "'.");
        }
        finally
        {
            if (Interlocked.Decrement(ref taskCount) == 0)
            {
                doneEvent.Set();
            }
        }
    }, null);
 
}
doneEvent.WaitOne();

  

 

posted on   Gu  阅读(193)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
点击右上角即可分享
微信分享提示