I created a really simple scenario where I recognized a really weird behavior which I cant understand.
Under following link I created an sequential implementation: http://ideone.com/B8JYeA Basically there are several big arrays with fixed size. The algorithm iterates through them and changes the value.
for(int i = 0; i < numberOfCells; i++) {
h0[i] = h0[i] + 1;
h1[i] = h1[i] + 1;
h2[i] = h2[i] + 1;
h3[i] = h3[i] + 1;
h4[i] = h4[i] + 1;
}
If I run it on my workstation it takes around 5 seconds.
I implemented the same in a parallel version. And 8 threads run it simultaneously. The code should be thread safe and there is no dependency between the threads.
But still the code runs around 4 times slower on my workstation: http://ideone.com/yfwVmr
final int numberOfThreads = Runtime.getRuntime().availableProcessors();
ExecutorService exec = Executors.newFixedThreadPool(numberOfThreads);
for(int thread = 0; thread < numberOfThreads; thread++) {
final int threadId = thread;
exec.submit(new Runnable() {
@Override
public void run() {
for(int i = threadId; i < numberOfCells; i += numberOfThreads) {
h0[i] = h0[i] + 1;
h1[i] = h1[i] + 1;
h2[i] = h2[i] + 1;
h3[i] = h3[i] + 1;
h4[i] = h4[i] + 1;
}
}
});
}
exec.shutdown();
Does anyone have an idea why this happens?
Edit: This problem differs to others because the reason why is probably a Caching Problem. How can I solve this caching problem?