2

I want to dump one Double array and two Long arrays into a bytebuffer. I could use a loop and do

double[] arr1 = new double[size];
long[] arr2 = new long[size];
long[] arr3 = new long[size];
for(int i=0;i<size;i++){
    buffer.putDouble(arr1[i]);
    buffer.putLong(arr2[i]);
    buffer.putLong(arr3[i]);
}

This doesn't seem efficient. Is there a way to balk dump them?

2
  • 2
    If I were you, I'd measure the throughput on your typical inputs before assuming that this is too slow. Commented Aug 3, 2011 at 16:42
  • You're not "bulk-dumping" them, you're interleaving the data. And there isn't anything in the JDK that knows how you want to do this. Commented Aug 7, 2011 at 12:20

1 Answer 1

2

If you truly want to "bulk dump" the data, rather than interleave it in some application-dependent manner, then you can create LongBuffer and DoubleBuffer views into your ByteBuffer. The general procedure is as follows:

  1. Call position() to set the position of the buffer to wherever you want to store the array (you'll have to calculate this based on the size of the primitives).
  2. Call slice() on the buffer to create a new buffer that shares the backing store but is offset.
  3. Call asLongBuffer() or asDouble() buffer on the new buffer.
  4. Call the bulk put() method on the buffer created in step 3.

This process is a convenience, not a performance enhancement. Unless you're talking tens of millions of elements, you're unlikely to see any improvement at all, and even then you're probably looking at microseconds.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.