Misunderstanding the difference between single-threading and multi-threading programming

前端 未结 6 1460
梦毁少年i
梦毁少年i 2021-01-31 16:12

I have a misunderstanding of the difference between single-threading and multi-threading programming, so I want an answer to the following question to make everything clear.

6条回答
  •  耶瑟儿~
    2021-01-31 16:51

    It depends.

    How many cpus do you have? How much I/O is involved in your tasks?

    If you have only 1 cpu, and the tasks have no blocking I/O, then the single threaded will finish equal to or faster than multi-threaded, as there is overhead to switching threads.

    If you have 1 cpu, but the tasks involve a lot of blocking I/O, you might see a speedup by using threading, assuming work can be done when I/O is in progress.

    If you have multiple cpus, then you should see a speedup with the multi-threaded implementation over the single threaded, since more than 1 thread can execute in parallel. Unless of course the tasks are I/O dominated, in which case the limiting factor is your device speed, not cpu power.

提交回复
热议问题