Skip to content

Instantly share code, notes, and snippets.

@chanjarster
Created February 22, 2019 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chanjarster/4598cb15bac03662c1dd66c8097c8282 to your computer and use it in GitHub Desktop.
Save chanjarster/4598cb15bac03662c1dd66c8097c8282 to your computer and use it in GitHub Desktop.
Test stdout performance
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;
}
}
}
FROM openjdk:8-alpine
COPY ConsolePrint.java /
RUN javac /ConsolePrint.java
WORKDIR /
ENTRYPOINT ["java", "ConsolePrint"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment