I have two functions which does the same thing - out of two sorted arrays A and B, return third array C with all numbers above median. I had to perform a benchmark for the two functions to see who is better, and then add to the first one threads and check again.
The first function was faster. When i tried to use multiple threads(in the first) i realized that performance was down when i used few threads(for example 4) and was better when i used single thread(i split the work equally between all four threads). At first i though it's related to higher cache miss issue(due to multiple threads), so i tried to set arrays to bigger sizes(millions) and it still happened.
First function
public static int[] bigThanMedianAlgo(int []a, int []b)
{
int [] ans = new int[a.length];
int num_threads = 4;
Thread [] threads = new Thread[num_threads];
long start_time = System.currentTimeMillis();
for (int i = 0; i < threads.length; i++)
{
threads[i] = new WorkerThread(a,b,ans, (ans.length/num_threads)*i, (ans.length/num_threads)*(i+1));
threads[i].start();
}
for (int i = 0; i < threads.length; i++)
{
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long estimated_time = System.currentTimeMillis() - start_time;
System.out.println("Run time: worker threads " + estimated_time + "ms");
return ans;
}
Second function
public static int[] bigThanMedianMerge(int[]a, int[] b)
{
int [] ans = new int[a.length];
long start_time = System.currentTimeMillis();
for(int i = a.length-1, j = b.length-1, r = 0; r < ans.length; r++)
{
if(a[i] > b[j])
{
ans[r] = a[i--];
}
else
{
ans[r] = b[j--];
}
}
long estimated_time = System.currentTimeMillis() - start_time;
System.out.println("Run time: Regular merge " + estimated_time + "ms");
return ans;
}
run method of WorkerThread:
public void run()
{
for (int i = start; i < end; i++)
{
c[i] = Math.max(a[i], b[a.length - i - 1]);
}
}
These are the result for array in size of 15 million items and 4 threads:
Run time: Regular merge 63ms
Run time: worker threads 24ms
These are the result for array in size of 15 million items and 1 thread:
Run time: Regular merge 63ms
Run time: worker threads 17ms
The only thing i changed between executions is the variable num_threads.
My computer has Intel i5-8400 CPU(6 cores) and i expected 4 threads will gain a better performances.
Can anyone suggest what might be the reason for that issue ? I can supply further information if needed.