I am developing an application, which can process images (using Dnn.forward() in OpenCV) at the same time. I assign 1 image per 1 thread. I run my application on Samsung S10+, which has 8 cores. Then, I compare performance among 1 thread, 2 threads, 4 threads, and 8 threads. I found out that the overall computation time is slightly reduced, but the computation time for each thread increased when I increase the number of threads.
For example,
void singleThread(){
new Thread(new Runnable() {
@Override public void run() {
Long start = System.currentTimeMillis();
processImage(image);
Long finish = System.currentTimeMillis();
Log.i(TAG, "Process time: " + ((finish - start)/1000.0) + " seconds");
}
});
}
void twoThread(){
new Thread(new Runnable() {
@Override public void run() {
Long start = System.currentTimeMillis();
processImage(image);
Long finish = System.currentTimeMillis();
Log.i(TAG, "Process time: " + ((finish - start)/1000.0) + " seconds");
}
});
new Thread(new Runnable() {
@Override public void run() {
Long start = System.currentTimeMillis();
processImage(image);
Long finish = System.currentTimeMillis();
Log.i(TAG, "Process time: " + ((finish - start)/1000.0) + " seconds");
}
});
}
void process(Mat image){
Mat blob = Dnn.blobFromImage(image, SCALE_FACTOR, new Size(WIDTH, HEIGHT), new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), false, false);
network = Dnn.readNetFromCaffe(configUri, weightPath);
network.setInput(blob);
Mat detection = network.forward();
}
As a result, "Process time" of a single thread is 2.5 seconds, but "Process time" of two thread are 3.5 and 4 seconds. But, when I use 8 threads, the "Process time" was 19 seconds.
My question is why each thread didn't take 2.5 seconds to process an image if I have 8 cores and I use 8 threads?. Why each thread took 19 seconds to process an image?.
Is it because there is no core handle other threads while processing image, and then it causes context switching overhead?
Thank you in advance.