Does multithreading always yield better performance than single threading?

前端 未结 7 1793
自闭症患者
自闭症患者 2020-12-25 08:03

I know the answer is No, here is an example Why single thread is faster than multithreading in Java? .

So when processing a task in a thread is triv

7条回答
  •  盖世英雄少女心
    2020-12-25 08:51

    As already mentionened in a comment by @Jim Mischel, you can use

    Amdahl's law

    to calculate this. Amdahl's law states that the speedup gained from adding processors to solve a task is

    enter image description here

    where

    N is the number of processors, and

    P is the fraction of the code that can be executed in parallel (0 .. 1)

    Now if T is the time it takes to execute the task on a single processor, and O is the total 'overhead' time (create and set up a second thread, communication, ...), a single thread is faster if

    T < T/S(2) + O

    or, after reordering, if

    O/T > P/2

    When the ratio Overhead / Execution Time is greater than P/2, a single thread is faster.

提交回复
热议问题