|
| 1 | +package io; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileOutputStream; |
| 5 | +import java.io.PrintStream; |
| 6 | +import java.util.UUID; |
| 7 | +import lombok.AllArgsConstructor; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +/** |
| 11 | + * @GitHub : https://github.com/zacscoding |
| 12 | + */ |
| 13 | +public class PrintstreamTest { |
| 14 | + |
| 15 | + @Test |
| 16 | + public void compare() throws Exception { |
| 17 | + //PrintType printType = PrintType.PRINTLN_APPEND; |
| 18 | + PrintType printType = PrintType.PRINTLN_BUILDER_INIT_CAPACITY; |
| 19 | + // PrintType printType = PrintType.PRINTF; |
| 20 | + int repeat = 10000; |
| 21 | + int threadCount = 5; |
| 22 | + File targetFile = new File("src/test/resources/io.txt"); |
| 23 | + if (targetFile.exists()) { |
| 24 | + targetFile.delete(); |
| 25 | + } |
| 26 | + |
| 27 | + PrintStream ps = new PrintStream(new FileOutputStream(targetFile)); |
| 28 | + String[] messages = new String[repeat]; |
| 29 | + |
| 30 | + for (int i = 0; i < repeat; i++) { |
| 31 | + messages[i] = UUID.randomUUID().toString() + UUID.randomUUID().toString() + UUID.randomUUID().toString(); |
| 32 | + } |
| 33 | + |
| 34 | + Task[] tasks = new Task[threadCount]; |
| 35 | + for (int i = 0; i < threadCount; i++) { |
| 36 | + tasks[i] = new Task(messages, ps, printType); |
| 37 | + } |
| 38 | + |
| 39 | + long start = System.currentTimeMillis(); |
| 40 | + for (int i = 0; i < threadCount; i++) { |
| 41 | + tasks[i].start(); |
| 42 | + } |
| 43 | + |
| 44 | + for (int i = 0; i < threadCount; i++) { |
| 45 | + tasks[i].join(); |
| 46 | + } |
| 47 | + long elapsed = System.currentTimeMillis() - start; |
| 48 | + |
| 49 | + System.out.printf("## print type : %s >> %d [MS]\n", printType, elapsed); |
| 50 | + |
| 51 | + // Output |
| 52 | + // ## print type : PRINTLN_APPEND >> 336 [MS] |
| 53 | + // ## print type : PRINTLN_BUILDER >> 378 [MS] |
| 54 | + // ## print type : PRINTF >> 459 [MS] |
| 55 | + } |
| 56 | + |
| 57 | + @AllArgsConstructor |
| 58 | + public static class Task extends Thread { |
| 59 | + |
| 60 | + private String[] message; |
| 61 | + private PrintStream ps; |
| 62 | + private PrintType printType; |
| 63 | + |
| 64 | + @Override |
| 65 | + public void run() { |
| 66 | + int repeat = message.length - 1; |
| 67 | + for (int i = 0; i < repeat; i++) { |
| 68 | + switch (printType) { |
| 69 | + case PRINTLN_APPEND: |
| 70 | + ps.println(message[i] + message[i + 1]); |
| 71 | + break; |
| 72 | + case PRINTLN_BUILDER_INIT_CAPACITY: |
| 73 | + ps.println( |
| 74 | + new StringBuilder(message[i].length() + message[i + 1].length() + 50) |
| 75 | + .append(message[i]) |
| 76 | + .append(message[i + 1]) |
| 77 | + .toString() |
| 78 | + ); |
| 79 | + break; |
| 80 | + case PRINTF: |
| 81 | + ps.printf("%s%s\n", message[i], message[i + 1]); |
| 82 | + |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private enum PrintType { |
| 89 | + PRINTLN_APPEND, |
| 90 | + PRINTLN_BUILDER_INIT_CAPACITY, |
| 91 | + PRINTF; |
| 92 | + } |
| 93 | +} |
| 94 | + |
0 commit comments