Friday, May 14, 2010

Java Logger with SimpleFormatter is ten time slower than OutputStream



I compared the time it takes to write a log file with several Java I/O classes and found that Logger takes 10 times longer than OutputStream.

I used OutputStream, RandomAccessFile and Logger to write a trace file of million lines. each line contained 250 characters and a prefix of the writing time. I repeated and collected the results 7 times.

The graph below summarize the results: vertical axe is the time it took to write the file in milliseconds and the horizontal is the sampling number.
Here is the I/O setup I used :
FileInputStream #1 - PrintWriter with auto flush enabled
FileInputStream #2 - PrintWriter with auto flush disabled
RandomAccessFile #1 - RandomAccessFile which write to file after each line
RandomAccessFile #2 - RandomAccessFile which store 10,000 lines in a buffer before writing to file
RandomAccessFile #3 - RandomAccessFile which store 200,000 lines in a buffer before writing to file
Logger #1 - Java util logger (JUL) with SimpleFormatter
Logger #2 - JUL with a Formatter that append the time to each message
Logger #3 - JUL with a Formatter that append the time and class name to each message

No comments:

Post a Comment