When will parallel increase performance

前端 未结 5 654
你的背包
你的背包 2021-02-06 16:11

I\'m trying to understand when the usage of parallel will increase the performance.
I tested it with a simple code that ran over 100,000 items in List<

5条回答
  •  暖寄归人
    2021-02-06 17:06

    To me, there are a couple of rules when you should think about parallelizing your code (and even then, you should still test to see if it is faster):

    1. The code you want to parallelize is computationally intensive. Just waiting for IO typically wont net you much benefit. It has to be something where you would definitely make use of a bunch of CPU time (like rendering an image).
    2. The code you want to parallelize is complex enough that the overhead of making the parallelization is less than the savings you get from distributing the code (ie, setting a string to string.Empty is incredibly simple and fast; you would need something much more complex per item to make it worth it)
    3. The code you want to parallelize is independent and has no dependencies on other items.

提交回复
热议问题