Created
          February 21, 2019 07:55 
        
      - 
      
- 
        Save chanjarster/b547d443aaf8f141e2d59a4cd01eaeaf to your computer and use it in GitHub Desktop. 
    Writing to stdout vs writing to file
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import java.io.BufferedOutputStream; | |
| import java.io.File; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| public class ConsolePrint { | |
| private static String 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 very very long line"; | |
| public static void main(String[] args) throws IOException { | |
| int count = Integer.valueOf(args[0]); | |
| long t1 = stdout(count); | |
| long t2 = file(count); | |
| System.out.println("lines: " + String.format("%,d", count)); | |
| System.out.println("stdout: " + String.format("%,d", t1) + " ms"); | |
| System.out.println("file: " + String.format("%,d", t2) + " ms"); | |
| } | |
| private static long stdout(int count) { | |
| long start = System.currentTimeMillis(); | |
| for (int i = 0; i < count; i++) { | |
| System.out.println(line); | |
| } | |
| long end = System.currentTimeMillis(); | |
| return end - start; | |
| } | |
| private static long file(int count) throws IOException { | |
| byte[] bytes = line.getBytes(); | |
| File tempFile = File.createTempFile("test", "log"); | |
| tempFile.deleteOnExit(); | |
| try ( | |
| FileOutputStream fileOutputStream = new FileOutputStream(tempFile); | |
| BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream, 1024 * 64); | |
| ) { | |
| long start = System.currentTimeMillis(); | |
| for (int i = 0; i < count; i++) { | |
| bufferedOutputStream.write(bytes); | |
| } | |
| bufferedOutputStream.flush(); | |
| long end = System.currentTimeMillis(); | |
| return end - start; | |
| } | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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