Java, JSP

IO vs NIO performance compare (성능 비교)

수앙 2013. 2. 14. 03:57

Java 1.4 버전에서 이미 NIO가 나왔음에도 일반 IO 쓰는게 대부분이다.

이번에 IO와 NIO의 성능 비교를 하고자 파일 복사 테스트를 하였으니 참고 시 유용하게 쓰였으면 한다(단, 걸린 시간만 체크).

 

테스트조건

- Java 버전: jdk1.6.0_39

- 파일크기: 232 Mbytes (src.zip)

- 버퍼사이즈: 8192 bytes

 

(1~6번까지 하나씩 테스트)

공통

private static File srcFile = new File("src.zip");
private static File desFile = new File("des.zip");
private static int bufferSize = 8192;

public static void main(String[] args) {
	StopWatch sw = null;
	try {
		sw = new StopWatch("nio test");
		sw.start();
		// 1. IO
		io();
		// 2. Scatter/Gather
		scatterGather();
		// 3. MappedByteBuffer
		mapBuffer();
		// 4. FileChannel + ByteBuffer
		channel();
		// 5. ByteBuffer transferTo
		transferTo();
		// 6.ByteBuffer transferFrom
		transferFrom();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		desFile.delete();
		sw.stop();
		System.out.println(sw.prettyPrint());
	}
}

 

 

1. IO (걸린시간: 약 9.8초)

public static void io() throws Exception {
	FileInputStream fis = new FileInputStream(srcFile);
	FileOutputStream fos = new FileOutputStream(desFile);
	
	BufferedInputStream bis = new BufferedInputStream(fis, bufferSize);
	BufferedOutputStream bos = new BufferedOutputStream(fos, bufferSize);
	
	int read = -1;
	while ((read = bis.read()) != -1) {
		bos.write(read);
	}
	
	bos.close();
	bis.close();
}

/* 파일복사 걸린시간
StopWatch 'nio test': running time (millis) = 9847
-----------------------------------------
ms     %     Task name
-----------------------------------------
09847  100%
*/

 

 

2. NIO Scatter/Gather (걸린시간: 약 0.5초)

public static void scatterGather() throws Exception {
	FileInputStream fis = new FileInputStream(srcFile);
	FileOutputStream fos = new FileOutputStream(desFile);
	
	ScatteringByteChannel sbc = fis.getChannel();
	GatheringByteChannel gbc = fos.getChannel();
	
	ByteBuffer bb = ByteBuffer.allocateDirect(bufferSize);
	while (sbc.read(bb) != -1) {
		bb.flip();
		gbc.write(bb);
		bb.clear();
	}
	
	fos.close();
	fis.close();
}

/* 파일복사 걸린시간
StopWatch 'nio test': running time (millis) = 468
-----------------------------------------
ms     %     Task name
-----------------------------------------
00468  100%
*/

 

 

3. NIO MappedByteBuffer (걸린시간: 약 3초)

public static void mapBuffer() throws Exception {
	FileInputStream fis = new FileInputStream(srcFile);
	FileOutputStream fos = new FileOutputStream(desFile);
	
	FileChannel fcIn = fis.getChannel();
	FileChannel fcOut = fos.getChannel();
	
	MappedByteBuffer mbb = fcIn.map(FileChannel.MapMode.READ_ONLY, 0, fcIn.size());
	fcOut.write(mbb);
	
	fos.close();
	fis.close();
}

/* 파일복사 걸린시간
StopWatch 'nio test': running time (millis) = 2995
-----------------------------------------
ms     %     Task name
-----------------------------------------
02995  100%
*/

 

 

4. NIO FileChannel + ByteBuffer (걸린시간: 약 3.2초)

public static void channel() throws Exception {
	FileInputStream fis = new FileInputStream(srcFile);
	FileOutputStream fos = new FileOutputStream(desFile);
	
	FileChannel fcIn = fis.getChannel();
	FileChannel fcOut = fos.getChannel();
	
	ByteBuffer bb = ByteBuffer.allocateDirect((int) fcIn.size());
	fcIn.read(bb);
	
	bb.flip();
	fcOut.write(bb);
	
	fos.close();
	fis.close();
}
	
/* 파일복사 걸린시간
StopWatch 'nio test': running time (millis) = 3233
-----------------------------------------
ms     %     Task name
-----------------------------------------
03233  100%
*/

 

5. NIO FileChannel transferTo (걸린시간: 약 2.8초)

public static void transferTo() throws Exception {
	FileInputStream fis = new FileInputStream(srcFile);
	FileOutputStream fos = new FileOutputStream(desFile);
	
	FileChannel fcIn = fis.getChannel();
	FileChannel fcOut = fos.getChannel();
	
	fcIn.transferTo(0, fcIn.size(), fcOut); // Unix > Windows
	
	fos.close();
	fis.close();
}

/* 파일복사 걸린시간
StopWatch 'nio test': running time (millis) = 2823
-----------------------------------------
ms     %     Task name
-----------------------------------------
02823  100%
*/

 

6. NIO FileChannel transferFrom (걸린시간: 약 2.9초)

public static void transferFrom() throws Exception {
	FileInputStream fis = new FileInputStream(srcFile);
	FileOutputStream fos = new FileOutputStream(desFile);
	
	FileChannel fcIn = fis.getChannel();
	FileChannel fcOut = fos.getChannel();
	
	fcOut.transferFrom(fcIn, 0, fcIn.size()); // Unix < Windows
	
	fos.close();
	fis.close();
}

/* 파일복사 걸린시간
StopWatch 'nio test': running time (millis) = 2920
-----------------------------------------
ms     %     Task name
-----------------------------------------
02920  100%
*/

 

끝.