I’ve notice that using a FloatBuffer is faster than using an array of float. Is this true ? could someone explains me why ?
++
Chman
I’ve notice that using a FloatBuffer is faster than using an array of float. Is this true ? could someone explains me why ?
++
Chman
Faster for what?
If it’s passing through JNI, then it’s because that’s what direct buffers are all about…
I mean that calling a buffer member (for example buffer.get(0) ) looks faster than calling myFloat[0]…
Haven’t done any tests, but I’d imagine that it is the bounds checking that is restricting the arrays? (on second thought, shouldn’t Buffers have bounds checks too?)
They have. You can look it up in the source.
See Buffer#checkBounds().
One of the first statements in FloatBuffer:
final float[] hb;
I don’t think they are faster if not using them for JNI or NIO.
In theory FloatBuffer (and others) should be faster if it was created as a view from a direct ByteBuffer:
ByteBuffer byteBuf = ByteBufferallocateDirect(9999);
FloatBuffer floatBuf = byteBuf.asFloatBuffer();
Ok, so :
ByteBuffer byteBuf = ByteBufferallocateDirect(9999);
FloatBuffer floatBuf = byteBuf.asFloatBuffer();
sould be faster than :
float [] = new float[9999];
Thanks for your replies, i’m going to make a little benchmark about it.
++
Chman
Is that true? I thought that arrays of primitives were allocated to the stack and not the heap so in essence they should be the same speed.
That’s what I assume from the jdk 1.4 API doc and that’s why I mentionned in theory:). I haven’t had time to try it yet.
We’ll see Chman’s results.
Interesting results :
Allocate float buffer : 20 milliseconds
Allocate float primitive : 60 milliseconds
Fill in float buffer : 50 milliseconds
Fill in float primitive : 20 milliseconds
Get in float buffer : 40 milliseconds
Get in float primitive : 30 milliseconds
So buffers seems to be only faster when allocating memory…
++
Chman
That’s kind of annoying, because they should be virtually identical. The machine code produced, after inlining and optimisation and bounds check elimination, should be exactly the same.
Grr.
Cas
Arrays even of primatives, are objectsm so they must be on the heap.
Are direct buffers zeroed like arrays have to be?
After optimization the access times should be the same. but prior to optimization buffers will have function call overhead for the accesses.
NIO Buffers are a new feature, they will get faster with future realeases of the JVM. JVM authors have had a long time to think about arrays and make them faster. Be patient.