Speed up loop using multithreading in C# (Question)

后端 未结 6 733
南笙
南笙 2020-12-13 15:40

Imagine I have an function which goes through one million/billion strings and checks smth in them.

f.ex:

foreach (String item in ListOfStrings)
{
            


        
6条回答
  •  [愿得一人]
    2020-12-13 16:36

    You could try the Parallel extensions (part of .NET 4.0)

    These allow you to write something like:

    Parallel.Foreach (ListOfStrings, (item) => 
        result.add(CalculateSmth(item));
    );
    

    Of course result.add would need to be thread safe.

提交回复
热议问题