可以使此LinQ语句运行多线程-使用更多的cpu内核 [英] Can this LinQ statement made run multi threading - using more cpu cores
问题描述
我写了下面的linq语句.但是由于行数太多,因此需要花费大量时间进行处理.我的cpu有8个核心,但由于运行单线程而仅使用1个核心.
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.
所以我想知道这最后的理由是否可以在多线程中运行?
So i wonder by any chance can this final stament run in multi threading ?
List<string> lstAllLines = File.ReadAllLines("AllLines.txt").ToList();
List<string> lstBannedWords = File.ReadAllLines("allBaddWords.txt").
Select(s => s.ToLowerInvariant()).
Distinct().ToList();
我在问下面的那个.那条线可以使用多线程吗?
I am asking the one below. Can that line work multi threading ?
List<string> lstFoundBannedWords = lstBannedWords.Where(s => lstAllLines.
SelectMany(ls => ls.ToLowerInvariant().Split(' ')).
Contains(s)).
Distinct().ToList();
C#5,网络框架4.5
C# 5 , netframework 4.5
推荐答案
The following snippet can perform that operation using the Parallel Tasks Library's Parallel.ForEach
method. The snippet below takes each line in the 'all-lines' file you have, splits it on spaces, and then searches each line for banned words. The Parallel-ForEach should use all available core's on your machine's processor. Hope this helps.
System.Threading.Tasks.Parallel.ForEach(
lstAllLines,
line =>
{
var wordsInLine = line.ToLowerInvariant().Split(' ');
var bannedWords = lstBannedWords.All(bannedWord => wordsInLine.Contains(bannedWord));
// TODO: Add the banned word(s) in the line to a master list of banned words found.
});
这篇关于可以使此LinQ语句运行多线程-使用更多的cpu内核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!