Android - fastest way to fill a [Byte|Float|Int]Buffer

I’ve been porting some code from desktop to Android and suddenly found my loading times were up to a couple of minutes. Turns out this was due to filling a ByteBuffer with initial pixels for a new texture.

Looping over all the pixels and doing relative ByteBuffer.put(byte) calls was taking ~10. But creating a temporary array, looping over that and then doing a single bulk ByteBuffer.put(byte[]) takes about 500ms. Filling the array with Arrays.fill() is about 200ms.

Have other people seen the same performance behaviour or is this a quirk of my device (S3)? What’s the fastest way to fill a byte buffer with unique values?

In Android, I recommend just using the built in Bitmap class, which you can use to fill a OpenGL texture using the GLUtils helper class. (I think it’s GLUtils, it might be something slightly different)

That’s normal behaviour on Android. The fastest way without going native is to put arrays instead of single values. They don’t have to be the size of the data, a reasonable sized temp buffer will do.

Hmm. That’s annoying. Because right now I’ve got 20 sprite objects, and simply generating their vertices and packing them into a ByteBuffer is running at 18fps. The same code happily runs 8k sprites at 60fps on my several years old MacBook Pro.

Surely Android isn’t this bad? I must be doing something wrong somewhere…

And you are doing this every frame?

Yeah. I’m using a Bubblemark setup for benchmarking, so all the sprites are moving all the time.

That’s a bit strange…i made myself a little test case and my Nexus 4 can render up to 66 individual instances of a 1089 vertices mesh at that frame rate (http://jpct.de/pix/many_wizards.png), all animated each frame which means ~71.000 updated vertices (plus ~71.000 normals…) each frame.

Which device are you testing this on?

Gnn. Just found out that when run normally on the phone (an S4) it runs at 60fps. Only when running with Eclipse attached is it super slow.

That’s particularly annoying since I had a very similar issue with my previous phone (an S2) and a different dev environment setup over a year ago and never found the root cause of it.

I never experienced that with any device. Are you running it in some kind of debug or tracking mode?

Just the usual debug via Eclipse and usb debugging. No funny settings on the developer options as far as I can tell.

Maybe it’s a issue with the USB connection itself. Maybe the port is running in 1.1 mode or something like this. This can happen because of faulty cables or components that are violating the USB specs. I would try to connect the device directly without using a hub or use another port…just to be sure.

Hmm, that is another possibility I’ve not thought of before. This is plugged direct into my macbook - i wonder if there’s somewhere that will tell me what speed it’s running at.

I don’t have a spare cable to hand right now, but I’ll certainly give that a try when I can. Thanks.

This is standard behavior. Running on a device with the debugger attached is waaaaay slow. I thought you learned this last time? :stuck_out_tongue: It is true for all devices. Either run with the Run button, not Debug, or disconnect the debugger after running. Eclipse has some settings that will automatically run in debug even if you press Run, eg if you have a breakpoint set, so keep an eye on it.