I have a function as below in Android which is a bit slow and is a bottleneck in my real-time app. I was thinking to somehow make it faster, maybe through GPU or any possible methods. What are ways to make it faster, more efficient using parallelization?
private int[] getArray (ByteBuffer byteBuffer) {
int[] array = new int[width * height];
FloatBuffer fb = byteBuffer.asFloatBuffer();
for (int i = 0; i < width* height; i++) {
float probability = 1 - floatBuffer.get();
if (probability > 0.9) {
array[i] = originalBuffer[i];
}
}
return array;
}
The context is for segmentation task. Basically, a ML model returns a mask in bytebuffer. I pass it to this function to make the background pink. Here it is for reference.
(Too long for a comment) But what you can easily do: Replace
by
And: I honestly don't know whether the compiler writes a machine code that calculates
width*heightin every loop pass for the abort criterion (I hope/think not). But give it a try: add the lineand then set
In this way, you have eliminated all arithmetic operations (apart of the indispensable
i++and<) in the loop.