Speed up loop using multithreading in C# (Question)

后端 未结 6 736
南笙
南笙 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:29

    The first question you must answer is whether you should be using threading

    If your function CalculateSmth() is basically CPU-bound, i.e. heavy in CPU-usage and basically no I/O-usage, then I have a hard time seeing the point of using threads, since the threads will be competing over the same resource, in this case the CPU.

    If your CalculateSmth() is using both CPU and I/O, then it might be a point in using threading.

    I totally agree with the comment to my answer. I made a erroneous assumption that we were talking about a single CPU with one core, but these days we have multi-core CPUs, my bad.

提交回复
热议问题