Can this LinQ statement made run multi threading - using more cpu cores

前端 未结 2 1304
南旧
南旧 2021-01-25 09:48

I have written the below linq statement. But it takes huge time to process since there are so many lines. My cpu has 8 cores but only using 1 core due to running single thread.

2条回答
  •  情话喂你
    2021-01-25 10:44

    There are rooms for performance improvements before resorting to AsParallel

    HashSet lstAllLines = new HashSet(
                                    File.ReadAllLines("AllLines.txt")
                                        .SelectMany(ls => ls.ToLowerInvariant().Split(' ')));
    
    List lstBannedWords = File.ReadAllLines("allBaddWords.txt")
                                        .Select(s => s.ToLowerInvariant())
                                        .Distinct().ToList();
    
    List lstFoundBannedWords = lstBannedWords.Where(s => lstAllLines.Contains(s))
                                        .Distinct().ToList();
    

    Since access to HasSet is O(1) and lstBannedWords is the shorter list, You may even not need any parallelism (TotalSearchTime=lstBannedWords.Count*O(1)). Lastly, you always have the option AsParallel

提交回复
热议问题