Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How can to copy a float array to a byte array with best performance?

807589Nov 15 2008 — edited Nov 15 2008
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 13 2008
Added on Nov 15 2008
2 comments
546 views