Not to nitpick, but in your sample, you're better off using ++i instead of i++ in the interests of speed and memory.
________________________________ From: Alan Kent <alan.j.k...@saic.com> To: javaposse@googlegroups.com Sent: Wed, July 21, 2010 10:18:01 AM Subject: [The Java Posse] Fastest way to parse data out of array of bytes? I was wondering what the fastest way (most highly performant) was to parse data structures serialized as an array of bytes. In my case its like a network packet (a true array of bytes where I need to peel of 1, 2, 4, and 8 byte integers, or variable length ASCII (8-bit) strings, etc.) Note I am after the FASTEST way to do this in Java (and/or Scala). Is it better to use the stream based classes, or is it better to do direct array accesses and do bit shift operations and masks with 0xff etc (to strip sign extension Java will do otherwise)? I suspect the stream based approaches would be slower. Sample code that sneaks in: byte b1 = buf[i++]; byte b2 = buf[i++]; byte b3 = buf[i++]; byte b4 = buf[i++]; int n = (b1 << 24) | ((b2 & 0xff) << 16) | ((b3 & 0xff) << 8) | (b4 & 0xff); // Must mask with 0xff or else sign extension will mess up the result. Java does not have unsigned bytes or ints! I was looking into array bounds checks, and what I found via Google indicated that hotspot leaves in array bounds checks as there was only a minor performance improvement found in practice. This lead me to wonder if there is a faster way to do the code since I would be doing lots of array accesses, each with a bounds check. Just curious! Thanks! Alan -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to javapo...@googlegroups.com. To unsubscribe from this group, send email to javaposse+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en. -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to javapo...@googlegroups.com. To unsubscribe from this group, send email to javaposse+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.