Reputation: 13
I have a large array in RAM and want to read data from it as fast as possible. Ignore any possible synchronization, I only wonder about the theory.
Is it faster to spread those reads over several threads than just using one?
Edit: the data points are about 20KB each and I can't predict in which order they are read.
Upvotes: 1
Views: 2349
Reputation: 1103
Generally speaking: yes but beware of cache misses.
Let's say that you have an int[]: consider partitioning it in ranges of subsequent elements and have each thread get a range of its own (thread1 get from 0 to 127, thread2 from 128 to 255, ...).
When you read one element of the array, the processor core executing the load is most likely to load some of the successive elements of the array in its cache, because most of the times they are going to be needed right after (immagine for (int i =0;;i++) do(arra[i])): if you don't partition your data in a coarse way, all this work is going to be wasted.
You can read more about this in the following articles from Joe Duffy:
Not strictly related: The 'premature optimization is evil' myth in particular the part "Understand the order of magnitude that matters"
As @Alex said, the general rule is that you have to always measure and never assume anything: efficient scalability via concurrency is a complex subject and requires a lot of deep understaing of the underlying architecture.
Upvotes: 2
Reputation: 8116
Just test it for your specific situation. Context switching of thread's is expensive afterall. And you might be just as fast using a single-threaded method.
Measure performance, don't guess it.
Upvotes: 0
Reputation: 5681
Technically yes. You can use more threads to read from different places in memory. CPU is faster so it could issues lots of reads, say one read per thread, until the result from the first read comes back. Then start processing the requests. This works assuming the RAM doesn't block; ie supports multiple reads at once. It, for example, your memory has only 1 input line and 1 output line, then it would be blocking and no amount of threads would help.
Now keep in mind what exactly you do with that data that you read. If you are sending it over a network synchronously or dumping it to HDD, it doesn't necessarily mean you should use multithreading to read the data since it will bottleneck on write_to_HDD/sendData.
If you have another CPU waiting to process the data retrieved then you could paralyze well. Read and process at the same time.
Upvotes: 1