0

I want to convert an array of 8 bytes (new byte[8]) to a long. I've found many solutions but which one is the fastest? I'm not sure how I can benchmark these solutions.

Here are a couple of solutions that look good to me:

  1. https://stackoverflow.com/a/60456641/5647659
  2. https://stackoverflow.com/a/29132118/5647659
  3. https://stackoverflow.com/a/27610608/5647659

Or maybe you have an even better solution?

Note: I don't need to convert them back to an array of bytes

Elie G.
  • 1,514
  • 1
  • 22
  • 38
  • If it is a bottleneck for your usage, benchmark the solutions to see which one is fastest. – jrook Oct 06 '20 at 16:02
  • 2
    *"I'm not sure how I can benchmark these solutions"* Then see: [How do I write a correct micro-benchmark in Java?](https://stackoverflow.com/q/504103/5221149) – Andreas Oct 06 '20 at 16:04
  • 1
    #3 would be the fastest. – Andreas Oct 06 '20 at 16:05
  • 2
    *"better solution?"* The simplest would be `ByteBuffer.wrap(arr).order(ByteOrder.BIG_ENDIAN).getLong()` where you can specify whether you want `BIG_ENDIAN` or `LITTLE_ENDIAN` byte order. The default is big-endian, also known as network-order. – Andreas Oct 06 '20 at 16:09

0 Answers0