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

前端 未结 2 1338
南旧
南旧 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:46

    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.
        });
    

提交回复
热议问题