

I have a function in android as shown below, it is a bit slow and is the bottleneck of my real time application. I'm thinking of making it faster somehow, maybe via gpu or any possible method. What are some ways to use parallelization to make it faster and more efficient?
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; }
Context is used to split tasks. Basically, the ml model returns a mask in a byte buffer. I pass it to this function to make the background pink. This is for reference only.
Correct Answer
(Comment is too long) But you can do it easily: Replace
float probability = 1 - floatbuffer.get(); if (probability > 0.9) {
Depend on
if (floatbuffer.get() < 0.1) {
And: I honestly don't know if the compiler wrote a machine code to calculate width*height
to calculate the abort criterion in each loop (I hope/thinkno) >). But try it: add this line
int product = width*height; int[] array = new int[product];
Then set up
for (int i = 0; i < product; i++) {
This way you eliminate all arithmetic operations in the loop (except the essential i
and ).
The above is the detailed content of Optimize the Android Java loop and copy some arrays whose elements are zeroed based on the FloatBuffer mask array. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft