Java 정수 + newLine 비교
실험 내용
- 자바에서 여러 정수값들을 각 줄에 출력하는 방법은 다음과 같이 여러 가지 방법 존재
- 정수를 i라는 변수에 넣었다고 가정할 때,
- 각 i마다
i + "\n"
을 출력 - 각 i마다
i
를 string으로 바꿔서 출력하고 BufferedWriter의 newLine 함수 이용 - 각 i마다 StringBuilder로
i
와 "\n"
을 합친 문자열을 출력 - StringBuilder 하나를 만들어 모든 i에 대해
i
와 \n
을 합쳐 한 문자열로 출력 System.out.println(i)
를 이용
- 이 방법들의 시간 소모 비교
- Java11, wsl2에서 실험
실험 코드
- 메인 함수
- option 변수에 따라 각 함수를 실행해서 파일에 기록
- 각 함수는 위에서 설명한 1 ~ 5번 연산을 수행하고 출력에 걸린 총 시간을 반환
public static void main(String[] args) throws IOException {
File file = new File("./Java11/test/Test1.txt");
FileWriter fw = new FileWriter(file, true);
long result;
int option = 1;
if (option == 1){
result = stringPlus();
fw.write("String Plus 결과 : " + result + "\n");
}
else if (option == 2){
result = stringNewLine();
fw.write("String New Line 결과 : " + result + "\n");
}
else if (option == 3){
result = stringBuild();
fw.write("String Build 결과 : " + result + "\n");
}
else if (option == 4){
result = stringBuildAll();
fw.write("String Build All 결과 : " + result + "\n");
}
else if (option == 5){
result = printLn();
fw.write("printLn 결과 : " + result + "\n");
}
fw.flush();
fw.close();
}
- stringPlus 함수
i + "\n"
연산 및 출력을 100만번 수행- BufferedWriter를 이용
public static long stringPlus() throws IOException {
long startTime = System.currentTimeMillis();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
for(int i = 0; i < 1000000; i++) {
bw.write(i + "\n");
}
bw.flush();
bw.close();
return System.currentTimeMillis() - startTime;
}
- stringNewline 함수
i
를 string으로 바꾸고 출력, newLine 출력을 100만번 수행- BufferedWriter를 이용
public static long stringNewLine() throws IOException {
long startTime = System.currentTimeMillis();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
for(int i = 0; i < 1000000; i++) {
bw.write(Integer.toString(i));
bw.newLine();
}
bw.flush();
bw.close();
return System.currentTimeMillis() - startTime;
}
- stringBuild 함수
- 각
i
에 대해 Stringbuilder를 이용해 i
와 \n
으로 string으로 만들고 출력하는 과정을 100만번 수행 - 매 번 stringBuilder를 초기화
- BufferedWriter를 이용
public static long stringBuild() throws IOException {
long startTime = System.currentTimeMillis();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
for(int i = 0; i < 1000000; i++) {
StringBuilder sb = new StringBuilder();
sb.append(i);
sb.append("\n");
bw.write(sb.toString());
}
bw.flush();
bw.close();
return System.currentTimeMillis() - startTime;
}
- stringBuildAll 함수
- StringBuilder 하나를 만듦
- 모든
i
에 대해 i
와 \n
을 StringBuilder에 합치는 과정을 100만번 수행 - BufferedWriter를 이용해 한 개의 string을 출력
public static long stringBuildAll() throws IOException {
long startTime = System.currentTimeMillis();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 1000000; i++) {
sb.append(i);
sb.append("\n");
}
bw.write(sb.toString());
bw.flush();
bw.close();
return System.currentTimeMillis() - startTime;
}
- printLn 함수
System.out.println(i)
함수를 100만번 수행
public static long printLn() throws IOException {
long startTime = System.currentTimeMillis();
for(int i = 0; i < 1000000; i++) {
System.out.println(i);
}
return System.currentTimeMillis() - startTime;
}
결과
- | 1번 | 2번 | 3번 | 4번 | 5번 |
---|
평균 소요 시간 (5회) | 3.8078s | 3.727s | 3.843s | 3.530s | 10.506s |
- 1, 2, 3번 방법은 유의미한 차이가 나지 않았음
- string을 모두 합친 다음 한 번에 출력하는 4번 함수가 약간 적게 걸리긴 했지만 이마저도 큰 차이는 아님
- 따라서 정수를 각 줄마다 출력하는 이 문제에서는 어떤 방식을 써도 큰 차이가 없을 것으로 보임
- 다만 5번처럼
System.out.println
함수를 사용하면 확연히 느리므로, 많은 양을 출력할때는 bufferedWriter를 사용해야 함 - 코드 전문