New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use direct bytebuffers #1

Closed
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
2 participants
@andresilva

andresilva commented Jun 29, 2013

A coworker referred me to your blog post (http://imranrashid.com/posts/profiling-bytebuffers/). I commented there about the possibility of using direct bytebuffers for increased performance, still I was a bit curious myself.

Here are the result of the original code:

Benchmark for raw arrays (300 calls in 4.452 s)
  Time:    14.71 ms   95% CI 14.12 ms - 15.31 ms   (n=20)
result = 1.19841533E14
Benchmark for byte buffer (20 calls in 2.244 s)
  Time:    107.7 ms   95% CI 102.6 ms - 112.8 ms   (n=19)
  Garbage: 0 ns   (n=1 sweeps measured)
result = 1.19841533E14
Benchmark for float buffer (60 calls in 2.402 s)
  Time:    40.19 ms   95% CI 38.25 ms - 42.13 ms   (n=20)
result = 1.19841533E14

And using direct bytebuffers:

Benchmark for raw arrays (140 calls in 2.074 s)
  Time:    14.30 ms   95% CI 13.59 ms - 15.02 ms   (n=20)
result = 1.19841533E14
Benchmark for byte buffer (140 calls in 2.609 s)
  Time:    18.05 ms   95% CI 17.02 ms - 19.08 ms   (n=19)
result = 1.19841533E14
Benchmark for float buffer (140 calls in 2.082 s)
  Time:    14.61 ms   95% CI 13.98 ms - 15.24 ms   (n=20)
result = 1.19841533E14

Endianness didn't since to influence the results that much, but I consistently got better results using native byteorder by a small margin (across 5 runs).

Direct byte buffers are also pretty good if you're interacting with C code (through JNI), since you can avoid copying buffers.

Cheers!

@squito

This comment has been minimized.

Show comment
Hide comment
@squito

squito Jul 2, 2013

Owner

Thanks for sending this! It was a great suggestion, the performance improvement is pretty significant. Do you know if this is just for reading, or is writing to direct byte buffers also faster? More experiments to try ...

One thing surprised me about your results, though. It seems for you, directly calling getFloat on a byte buffer was about the same as reading via view buffer. In all my tests, the view buffers were significantly better.

I'm not going to apply this PR, because I've made a bunch of other changes to the code, and I want to run the heap & direct byte buffers side-by-side, but I really appreciate you doing this!

Owner

squito commented Jul 2, 2013

Thanks for sending this! It was a great suggestion, the performance improvement is pretty significant. Do you know if this is just for reading, or is writing to direct byte buffers also faster? More experiments to try ...

One thing surprised me about your results, though. It seems for you, directly calling getFloat on a byte buffer was about the same as reading via view buffer. In all my tests, the view buffers were significantly better.

I'm not going to apply this PR, because I've made a bunch of other changes to the code, and I want to run the heap & direct byte buffers side-by-side, but I really appreciate you doing this!

@squito squito closed this Jul 2, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment