Is it possible to use threads to speed up file reading?

后端 未结 6 1099
情话喂你
情话喂你 2020-12-05 07:47

I want to read a file as fast as possible (40k lines) [Edit : the rest is obsolete].

Edit: Andres Jaan Tack suggested a solution based on one thread per file, and I

6条回答
  •  广开言路
    2020-12-05 08:29

    [Edit: original question asked if launching up to 40,000 threads would speed up file read]

    What you suggest would most likely slow down the access due to the overhead of creating threads and context switching. More threads only help if you are

    1) computationally bound and you have extra cores that could help with the work

    2) blocking and other threads could work while waiting for others to unblock

    3) you have a very clever algorithm that makes use of cache behavior

    Most likely your speed is bound by disk and/or memory bandwidth not computational limits so a single execution thread would be able to max those out.

提交回复
热议问题