search
HomeJavaOptimize the Android Java loop and copy some arrays whose elements are zeroed based on the FloatBuffer mask array

Question content

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!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft