When will parallel increase performance

前端 未结 5 656
你的背包
你的背包 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 16:55

    A loop will benefit from parallelism when the computations performed in parallel is greater than the overhead of using parallelism (thread startup, thread switching, communication, thread contention, etc). Your test seems to imply that parallism should benefit trivial calculations, it doesn't. What it is showing you is that there is overhead to paralellism. The amount of work must be greater (and ususally significantly greater) than the overhead for you to see any benefit.

    You also seem to dismiss testing. Testing is the only way you will know if parallism is buying you anything. You don't need to performance test every loop, just the performance critical ones. If the loop isn't performance critical why even bother making it parallel? And if it is critial enough to spend time making it parallel you better have a test in place to ensure that you are getting benefit from your labor and regression tests to ensure some clever programmer later doesn't destroy your work.

提交回复
热议问题