순간을 성실히, 화려함보단 꾸준함을

[자바 입출력] System.out.println()을 쓰지 말자! 본문

알고리즘,SQL/백준,BOJ

[자바 입출력] System.out.println()을 쓰지 말자!

폭발토끼 2021. 12. 12. 16:04

오늘 새롭게 알게 된 사실.....자바에서 입출력 할때 입력할때는 Scanner를 사용하지 말아야 된다는 건 알고 있었지만, sysout 또한 사용하면 안된다는 사실을 알 수 있었네요...ㅠㅠㅠ

 

1)sysout

package com.boj.practice;
/**
 * IO Template reference : 류호석
 */

import java.io.*;
import java.nio.Buffer;
import java.util.*;
import java.util.concurrent.DelayQueue;

import static java.lang.Math.*;

public class Main{
    static FastReader scan = new FastReader();

    public static void main(String[] args) throws IOException{
        int t=1;
        //t = scan.nextInt();
        while(t-->0)
            solve();
    }
    static void solve() throws IOException{
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        long beforeTime = System.currentTimeMillis();
        for (int i = 0; i < 1_000_000_000; i++) {
            System.out.print("");
            //bw.write("");
        }
        //bw.flush();
        long afterTime = System.currentTimeMillis();
        System.out.println("시간차이(m) : " + (afterTime-beforeTime));
    }

    static class FastReader{
        BufferedReader br;
        StringTokenizer st;

        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        public FastReader(String s) throws FileNotFoundException {
            br = new BufferedReader(new FileReader(new File(s)));
        }
        String next(){
            while(st==null || !st.hasMoreElements()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt(){
            return Integer.parseInt(next());
        }
        double nextDouble(){
            return Double.parseDouble(next());
        }
        long nextLong(){
            return Long.parseLong(next());
        }
        String nextLind(){
            String str="";
            try{
                str=br.readLine();
            }catch (IOException e){
                e.printStackTrace();
            }
            return str;
        }
    }
}

시간차이(m) : 32021

 

2)BufferedWriter 사용

package com.boj.practice;
/**
 * IO Template reference : 류호석
 */

import java.io.*;
import java.nio.Buffer;
import java.util.*;
import java.util.concurrent.DelayQueue;

import static java.lang.Math.*;

public class Main{
    static FastReader scan = new FastReader();

    public static void main(String[] args) throws IOException{
        int t=1;
        //t = scan.nextInt();
        while(t-->0)
            solve();
    }
    static void solve() throws IOException{
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        long beforeTime = System.currentTimeMillis();
        for (int i = 0; i < 1_000_000_000; i++) {
            //System.out.print("");
            bw.write("");
        }
        bw.flush();
        long afterTime = System.currentTimeMillis();
        System.out.println("시간차이(m) : " + (afterTime-beforeTime));
    }

    static class FastReader{
        BufferedReader br;
        StringTokenizer st;

        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        public FastReader(String s) throws FileNotFoundException {
            br = new BufferedReader(new FileReader(new File(s)));
        }
        String next(){
            while(st==null || !st.hasMoreElements()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt(){
            return Integer.parseInt(next());
        }
        double nextDouble(){
            return Double.parseDouble(next());
        }
        long nextLong(){
            return Long.parseLong(next());
        }
        String nextLind(){
            String str="";
            try{
                str=br.readLine();
            }catch (IOException e){
                e.printStackTrace();
            }
            return str;
        }
    }
}

시간차이(m) : 1143

 

참고: https://gist.github.com/psayre23/c30a821239f4818b0709