Hi all!
In C++, we can copy a float array to a byte as following:
float*myfloat=new float[size];
char*myChar=new char[size*4];
memcpy(myChar,myFloat,4*size);
And the performance of memcpy is very good.
But in Java, how can we do this?
I use an ByteBuffer to store the data, and put float array to it as following:
public void putFloatArray(float[] value) {
int arrayLength = 0;
if (value != null)
arrayLength = value.length;
this.putInt(arrayLength);
if(arrayLength>0)
{
this.resize(arrayLength*Float.SIZE/Byte.SIZE);
FloatBuffer floatBuffer=buffer.asFloatBuffer();
floatBuffer.put(value);
this.position(this.position()+arrayLength*Float.SIZE/Byte.SIZE);
}
}
However the performance is so bad. The speed is 1/10 of speed of buffer.put(byte[]value).
I attached the example here. Could you review it?
http://www.mediafire.com/?kf3jex3q4dn