<< 온라인에서 많이 뜨는 용어 42 | | ROME 라이브러리를 이용하여 RSS 피드 읽어 오기 >>

Java I/O Performance 성능 향상 코딩

Sun Website의 아티클을 가지고 테스트 프로그램을 만들어 어느것이 성능이 좋은 코딩 방법인지 실제 테스트를 해 보았습니다.

copyEx1() 함수는 Raw File Stream형태의 방식을 구사하여 제일 늦게 나왔고, 그 다음으로 copyEx2() 함수는 Buffered Streams을 사용해서 파일 카피를 했습니다. 이게 가장 많이 사용하죠. 대부분의 개발자들이 무의식적으로 이 방법을 사용합니다. ^^ 이것 보다 나은 방법이 있습니다.
copyEx3, ,4 5() 함수는 Custom Buffer 방식인데 copyEx3() 함수는 멀티 쓰레드 방식에서 사용 가능합니다. 그리고 가장 성능이 우수하다는 걸 실행 결과를 보면 아실 것입니다. 그리고 멀티 쓰레드 방식일 경우에는 병목현상이 발생하니 synchronized 코드를 잘 활용하세여.
copyEx4()는 일반 버퍼를 사용한 경우입니다. copyEx5()는 Buffered Streams 방식을 추가해 보았는데 3,4보다는 성능이 떨어지는 것을 확인할 수 있습니다.

아래는 성능 테스트를 한 소스입니다.

1. 테스트 소스

package client;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyExamples
{
// Thread buffer size
private static final int BUFF_SIZE = 1024 * 4;
private static final byte[] buffer = new byte[BUFF_SIZE];


// Thread buffer variable
private static final ThreadLocal<byte[]> buffCache =
new ThreadLocal<byte[]>()
{
@Override
protected byte[] initialValue() {
return new byte[BUFF_SIZE];
}
};

private CopyExamples()
{
}

public static void copyEx1(String from, String to) throws IOException
{
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(from);
out = new FileOutputStream(to);
while (true) {
int data = in.read();
if (data == -1)
break;
out.write(data);
}
in.close();
out.close();
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}

// Buffered Streams
public static void copyEx2(String from, String to) throws IOException
{
InputStream in = null;
OutputStream out = null;
try {
InputStream inFile = new FileInputStream(from);
in = new BufferedInputStream(inFile);
OutputStream outFile = new FileOutputStream(to);
out = new BufferedOutputStream(outFile);
while (true) {
int data = in.read();
if (data == -1)
break;
out.write(data);
}
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}


public static void copyEx3(String from, String to) throws IOException
{
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(from);
out = new FileOutputStream(to);
byte[] buffer = buffCache.get();
int amountRead;
while ((amountRead = in.read(buffer)) != -1) {
out.write(buffer, 0, amountRead);
}
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}

public static void copyEx4(String from, String to) throws IOException
{
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(from);
out = new FileOutputStream(to);
while (true) {
synchronized (buffer) {
int amountRead = in.read(buffer);
if (amountRead == -1) {
break;
}
out.write(buffer, 0, amountRead);
}
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}


public static void copyEx5(String from, String to) throws IOException
{
InputStream in = null;
OutputStream out = null;
try {
InputStream inFile = new FileInputStream(from);
in = new BufferedInputStream(inFile);
OutputStream outFile = new FileOutputStream(to);
out = new BufferedOutputStream(outFile);
byte[] buffer = buffCache.get();
int amountRead;
while ((amountRead = in.read(buffer)) != -1) {
out.write(buffer, 0, amountRead);
}
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}

public static void main(String[] args)
{
try {
long start1 = System.currentTimeMillis();
CopyExamples.copyEx1("D:\\temp\\march.pdf",
"D:\\temp\\march1.pdf");
System.out.println("copyEx1 Duration : " +
(System.currentTimeMillis() - start1));

long start2 = System.currentTimeMillis();
CopyExamples.copyEx2("D:\\temp\\march.pdf",
"D:\\temp\\march2.pdf");
System.out.println("copyEx2 Duration : " +
(System.currentTimeMillis() - start2));

long start3 = System.currentTimeMillis();
CopyExamples.copyEx3("D:\\temp\\march.pdf",
"D:\\temp\\march3.pdf");
System.out.println("copyEx3 Duration : " +
(System.currentTimeMillis() - start3));

long start4 = System.currentTimeMillis();
CopyExamples.copyEx2("D:\\temp\\march.pdf",
"D:\\temp\\march4.pdf");
System.out.println("copyEx4 Duration : " +
(System.currentTimeMillis() - start4));

long start5 = System.currentTimeMillis();
CopyExamples.copyEx2("D:\\temp\\march.pdf",
"D:\\temp\\march5.pdf");
System.out.println("copyEx5 Duration : " +
(System.currentTimeMillis() - start5));
} catch (Exception e) {
System.out.println(e);
}
}
}
2. 실행 결과

copyEx1 Duration : 8531
copyEx2 Duration : 109
copyEx3 Duration : 16
copyEx4 Duration : 94
copyEx5 Duration : 109
태그 :



코멘트 달기 Send a TrackBack