I'm writing a program that reads a file (uses custom buffer, 8KB), then finds a keyword in that buffer. Since Java provides two type of streams: character & byte, I've implemented this using both byte[]
and char[]
for buffering.
I just wonder, which would be faster and better for performance, since a char
is 2 byte
and when using Reader
to read up char[]
, the Reader
will perform converting back from byte
to char
, which I think could make it slower than using only byte[]
.
Using a byte array will be faster:
You don't have the bytes to characters decoding step, which is at least a copy loop, and possibly more depending on the Charset used to do the decoding.
The byte array will take less space, and hence save CPU cycles in GC / initialization.
However:
Unless you are searching huge files, the difference is unlikely to be significant.
The byte array approach could FAIL if the input file is not encoded in an 8 bit character set. And even if it works (as it does for UTF-8 & UTF-16) there are potential issues with matching characters that span buffer boundaries.
(The reason that byte-wise treatment works for UTF-8 and UTF-16 is that the encoding makes it easy to distinguish between the first unit (byte or short) and subsequent units of an encoded character.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With