I have a float[][] of pixels of an image, I will perform an operation on each pixel of this image. My current implementation is as follows:
float[][] pixels = image.pixels;
for(x = 0; x < pixels[0].length; x++) {
for(y = 0; y < pixels.length; y++) {
//perform operation on pixel
}
}
This implementation is very slow and I would like to speed it up by parallelising the for loops, how would I go about doing this?
You have 2 main options.
1) Would be using streams (Java8 +) where depending on what you are doing in your operations, the parallelism is mostly done for you, as you can make use of a parallelstream (NB: for small datasets, there is an overhead associated with it & sequential streams may in fact deliver comparable performance).
2)Alternatively, you can do the parallelism yourself by using threads, and delegate subsections of the datasets to different threads, if you are doing more lightweight tasks, you can use ForkJoin threads or else just normal threads.