Skip to content

Instantly share code, notes, and snippets.

@chanjarster
Created February 22, 2019 05:20

Revisions

  1. chanjarster created this gist Feb 22, 2019.
    54 changes: 54 additions & 0 deletions ConsolePrint.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;

    public class ConsolePrint {

    private static String line1 = "[stdout] very very long line very very long line very very long line very very long line very very long line very very long line very very long line very very long line";
    private static String line2 = "[file] very very long line very very long line very very long line very very long line very very long line very very long line very very long line very very long line\n";
    private static String line3 = "[/dev/stdout] very very long line very very long line very very long line very very long line very very long line very very long line very very long line very very long line\n";

    public static void main(String[] args) throws IOException {
    int count = Integer.valueOf(args[0]);

    long t1 = stdout(count);

    File f1 = File.createTempFile("test", "log");
    f1.deleteOnExit();
    long t2 = file(f1, line2, count);

    File f2 = new File("/dev/stdout");
    long t3 = file(f2, line3, count);

    System.out.println("lines: " + String.format("%,d", count));
    System.out.println("System.out.println: " + String.format("%,d", t1) + " ms");
    System.out.println("file: " + String.format("%,d", t2) + " ms");
    System.out.println("/dev/stdout: " + String.format("%,d", t3) + " ms");
    }

    private static long stdout(int count) {
    long start = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
    System.out.println(line1);
    }
    long end = System.currentTimeMillis();
    return end - start;
    }

    private static long file(File file, String line, int count) throws IOException {
    byte[] bytes = line.getBytes();
    try (
    FileOutputStream fos = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(fos, 1024 * 4);
    ) {
    long start = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
    bos.write(bytes);
    }
    bos.flush();
    long end = System.currentTimeMillis();
    return end - start;
    }
    }
    }
    5 changes: 5 additions & 0 deletions Dockerfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    FROM openjdk:8-alpine
    COPY ConsolePrint.java /
    RUN javac /ConsolePrint.java
    WORKDIR /
    ENTRYPOINT ["java", "ConsolePrint"]