Writing human readable data faster than binary.
OverviewIt is conventional wisdom that writing binary data is faster than writing human readable data. While binary is typically faster, are all means of writing text slower than standard approaches in writing binary?
For the purposes of this test I compare writing long values in binary as a fixed width fields (the most CPU efficient binary format) and human readable format with a '\n' separator.
Techniques of writing and reading text and binaryTwo of the most common ways to write binary and text is to use DataOutputStream/DataInputStream and using PrintWriter/BufferedReader.
I compare these with using ByteBuffer for binary and lower level memory access using the Unsafe class.
The CodeThe code is too long include here so I have made it available on Google Code
The resultsI ran this test on a Xeon 2.67 GHz with Java 6 update 25 on Windows Vista.
Unsafe binary: Typically took 4.2 ns to write/read per long. ByteBuffer binary: Typically took 10.4 ns to write/read per long. Unsafe text: Typically took 42.5 ns to write/read per long. DataStream binary: Typically took 68.7 ns to write/read per long. Print text: Typically took 336.2 ns to write/read per long.
This test writes a sequence of 128K long values and reads them back. The average time is taken 1001 times and the median reported.
What is interesting is that it is possible to write/read a text form which is faster than using a Data stream. It is significantly faster than using the standard Java code to do this.
Post a Comment