GBa
GBa

Reputation: 18387

Would multiple threads speed up code reading from disk into memory?

If I had code which were to read every word of a file into an ArrayList or HashSet, would it be any faster to split the code into multiple worker threads and assign each a chunk of the file to work on (assuming multiple cores)? My gut says no since the I/O would usually be the bottleneck rather than the CPU in a case like this.

Upvotes: 1

Views: 223

Answers (2)

Brian Gideon
Brian Gideon

Reputation: 48949

It depends. Your line of thinking that the IO is going to be the bottleneck could be correct since a lot of disks work in a serial fashion. But, what if that disk were special like an SSD or a RAID that really did support concurrent access? Also, if there were a significant amount of CPU bound post processing that needs to be done with the data then you could get that going concurrently while another batch of data is being read. Do not write off the concurrent options so quickly!

Upvotes: 2

Marc B
Marc B

Reputation: 360592

The IO channel off a regular drive is usually much faster than what the physical media itself can provide, so IO won't be the bottleneck. With magnetic media (aka a standard harddrive), you'd make the disk thrash like crazy as the heads seek to the various places you're reading from. Performance would be abysmal, the equivalent of a shopping cart rolling down an empty 6lane freeway.

Solid state drives don't suffer from the seek penalty, but they're not widespread (or affordable) enough yet to count for much yet.

Upvotes: 3

Related Questions