Java - Performance of File Reading
By admin on Feb 6, 2008 in Java, Programming
Here is a simple program that I wrote to prove that file reading using buffered stream is indeed performing better, especially when you are reading large binary file.
import java.io.*; public class FilePerformance { protected static String LARGE_BINARY_FILE_NAME = "c:/temp/large.bin"; protected static String LARGE_TEXT_FILE_NAME = "c:/temp/large.txt"; public void createLargeBinaryFile() throws Exception { BufferedOutputStream os = new BufferedOutputStream( new FileOutputStream(LARGE_BINARY_FILE_NAME)); byte[] b = new byte[]{0xC, 0xA, 0xF, 0xE, 0xB, 0xA, 0xB, 0xE}; int c = 2000000; for (int i = 0; i < c; i++) { os.write(b); os.flush(); } } public void createLargeTextFile() throws Exception { FileWriter outFile = new FileWriter(LARGE_TEXT_FILE_NAME); PrintWriter out = new PrintWriter(outFile); int c = 1000000; for (int i = 0; i < c; i++) { out.write("Sample large text file - " + i); } out.close(); } public void testWithBuffer() throws Exception { long startTime = System.currentTimeMillis(); InputStream in = null; byte[] out = new byte[0]; in = new BufferedInputStream( new FileInputStream(LARGE_BINARY_FILE_NAME)); int bufLen = 20000 * 1024; byte[] buf = new byte[bufLen]; byte[] tmp = null; int len = 0; while ((len = in.read(buf, 0, bufLen)) != -1) { System.out.println("Total bytes read: " + len); } in.close(); System.out.println("Total time with buffer: " + ((System.currentTimeMillis() - startTime)) + " miliseconds "); } public void testWithoutBuffer() throws Exception { long startTime = System.currentTimeMillis(); InputStream in = null; byte[] out = new byte[0]; in = new FileInputStream(LARGE_BINARY_FILE_NAME); int bufLen = 20000 * 1024; byte[] buf = new byte[bufLen]; byte[] tmp = null; int len = 0; while ((len = in.read(buf, 0, bufLen)) != -1) { System.out.println("Total bytes read: " + len); } in.close(); System.out.println("Total time without buffer: " + ((System.currentTimeMillis() - startTime)) + " miliseconds "); } public static void main(String[] args) { try { FilePerformance fp = new FilePerformance(); //fp.createLargeBinaryFile(); //fp.createLargeTextFile(); fp.testWithoutBuffer(); fp.testWithBuffer(); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } } }
The output
Total bytes read: 16000000 Total time without buffer: 141 miliseconds Total bytes read: 16000000 Total time with buffer: 125 miliseconds
tejinder | May 15, 2008 | Reply
But if you change the order of method call in main method. i.e. first call fp.testWithBuffer and then after call fp.testWithoutBuffer, you’ll get opposite result. Wich ever method you call first, will take less time. It just just becouse disk pointer are already set before second method call.