Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 63 ยท Contest Toolkit

Fast Input and Output in C++, Java and Python, and Reading from Files

Make contest I/O fast and safe: C++ sync_with_stdio, scanf and custom readers, Java BufferedReader and FastReader, Python sys.stdin, plus file input and output on your own machine.

When a problem has 10^6 numbers to read, the algorithm is rarely the slow part. Reading and printing are. A solution with the perfect idea can still receive "Time Limit Exceeded" because it spent two seconds in cin or Scanner. Fast input and output is a small, mechanical skill, and it is worth learning once for every language you submit in.

This lesson covers the fast idiom for C++, Java and Python, how to read from a file on your own computer instead of typing sample input by hand, and how to switch that off before you submit. The Python parts are the same ideas as in Python for Contests with the timings made visible, and the C++ and Java sections show the code you would put in a template.

The runnable examples on this page are in Python, the language of this course and of the in-page runner. The C++ and Java listings are plain code blocks. They are complete programs you can paste into a file and compile yourself.

Why standard input is slow by default

Every call that reads from the keyboard or a pipe crosses a boundary: your program asks the operating system for bytes, and the language wraps them in objects. Doing that once per number is expensive. Three habits cause most of the slowness:

  1. Tiny reads. One system call per number instead of one per 64 KB.
  2. Synchronization and flushing. C++ streams stay in step with C's stdio, and std::endl flushes the output on every line.
  3. Heavy parsing. Scanner in Java runs regular expressions on every token.

All the fast idioms below do the same thing: read big blocks, split them in memory, and write the answer once.

C++

The two-line fix for cin and cout

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        long long x;
        cin >> x;
        sum += x;
    }
    cout << sum << '\n';
}
  • ios::sync_with_stdio(false) cuts the link between C++ streams and C's stdin/stdout, so the stream can keep its own big buffer. After this line, never mix cin with scanf, or cout with printf, in the same program, because the two buffers would interleave in surprising orders.
  • cin.tie(nullptr) stops cin from flushing cout before every read. That flush exists so an interactive prompt appears before the program waits for input. In a normal contest problem nothing needs it. In an interactive problem you must flush on purpose, so leave the tie in place (or flush yourself).
  • Print '\n' instead of std::endl. endl writes a newline and flushes, and a flush per line over 10^6 lines is slow.

With these two lines cin and cout are usually fast enough for inputs up to a few million numbers.

scanf and printf

If you prefer C-style I/O, scanf and printf are fast without any setup and give exact control of formats:

C++
#include <cstdio>

int main() {
    int n;
    scanf("%d", &n);
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        long long x;
        scanf("%lld", &x);
        sum += x;
    }
    printf("%lld\n", sum);
}

Use %d for int, %lld for long long, %lf for double in scanf (but %f or %lf in printf) and %s for a word. Do not forget the & in scanf: leaving it out compiles with only a warning and crashes at run time.

A custom reader: getchar_unlocked and fread

When the input is huge (tens of millions of characters) or the time limit is tight, read the bytes yourself. getchar is thread-safe, which means it takes a lock for every character. On Linux, getchar_unlocked skips the lock and is much faster for a single-threaded program:

C++
#include <cstdio>

static inline int readInt() {
    int c = getchar_unlocked();
    while (c != '-' && (c < '0' || c > '9')) c = getchar_unlocked();
    int sign = 1;
    if (c == '-') {
        sign = -1;
        c = getchar_unlocked();
    }
    int x = 0;
    while (c >= '0' && c <= '9') {
        x = x * 10 + (c - '0');
        c = getchar_unlocked();
    }
    return x * sign;
}

int main() {
    int n = readInt();
    long long sum = 0;
    while (n--) sum += readInt();
    printf("%lld\n", sum);
}

getchar_unlocked is a POSIX function. Windows compilers may call it _getchar_nolock, and a few judges do not provide it at all. The most portable fast reader pulls a large block with fread into your own buffer and walks it:

C++
#include <cstdio>

static char buf[1 << 25];  // 32 MB: enough for a typical large input
static size_t len = 0, pos = 0;

static inline int nextByte() {
    if (pos == len) {
        len = fread(buf, 1, sizeof buf, stdin);
        pos = 0;
        if (len == 0) return -1;
    }
    return buf[pos++];
}

Refilling the buffer in a loop (as above) means the buffer size only affects speed, not correctness. For output, build a std::string or a char array and write it once with fwrite or puts.

Quick C++ rules

Do Avoid
ios::sync_with_stdio(false); cin.tie(nullptr); Mixing cin and scanf after that line
'\n' std::endl in loops
One big string or vector of output, printed once cout inside a hot loop with flushes
fread or getchar_unlocked for giant inputs Reading a character at a time with getchar

Java

Scanner is the slow choice

Scanner is convenient and slow. It splits input with regular expressions, which can cost several seconds for 10^6 numbers. Do not use it in contests unless the input is tiny.

BufferedReader with StringTokenizer

The standard fast reader wraps standard input in a BufferedReader (reads in blocks), reads one line at a time, and splits it with StringTokenizer. Put it in a small class and copy it into every solution:

Java
import java.io.*;
import java.util.*;

public class Main {
    static class FastReader {
        private final BufferedReader br;
        private StringTokenizer st;

        FastReader(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                String line = br.readLine();
                if (line == null) return null;
                st = new StringTokenizer(line);
            }
            return st.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }
    }

    public static void main(String[] args) throws IOException {
        FastReader in = new FastReader(System.in);
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int n = in.nextInt();
        long sum = 0;
        for (int i = 0; i < n; i++) {
            sum += in.nextLong();
        }
        out.println(sum);
        out.flush();
    }
}

The loop in next() skips blank lines and lines with extra spaces, so the layout of the input does not matter. The output side matters just as much: System.out.println in a loop is slow, because System.out flushes often. A PrintWriter wrapped around a BufferedWriter collects everything and writes when you call flush() (or close()). Forgetting out.flush() is the classic mistake: the program runs, the judge sees empty output and answers Wrong Answer.

StreamTokenizer and a byte-level reader

Two faster options exist. StreamTokenizer parses numbers without creating strings:

Java
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        st.nextToken();
        int n = (int) st.nval;
        long sum = 0;
        for (int i = 0; i < n; i++) {
            st.nextToken();
            sum += (long) st.nval;
        }
        System.out.println(sum);
    }
}

Its numbers are doubles, so values beyond about 2^53 (9 * 10^15) lose precision. It is also unsuitable for words that look like numbers. For long values near 10^18, use the BufferedReader reader above or the byte-level reader below.

The fastest approach reads raw bytes through a DataInputStream and builds numbers by hand, exactly like the C++ fread reader:

Java
import java.io.*;

public class Main {
    private static final int BUFFER_SIZE = 1 << 16;
    private static final DataInputStream din = new DataInputStream(System.in);
    private static final byte[] buffer = new byte[BUFFER_SIZE];
    private static int bufferPointer = 0, bytesRead = 0;

    private static byte read() throws IOException {
        if (bufferPointer == bytesRead) {
            bytesRead = din.read(buffer, 0, BUFFER_SIZE);
            bufferPointer = 0;
            if (bytesRead <= 0) return -1;
        }
        return buffer[bufferPointer++];
    }

    static long nextLong() throws IOException {
        long ret = 0;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = c == '-';
        if (neg) c = read();
        while (c >= '0' && c <= '9') {
            ret = ret * 10 + c - '0';
            c = read();
        }
        return neg ? -ret : ret;
    }

    public static void main(String[] args) throws IOException {
        int n = (int) nextLong();
        long sum = 0;
        for (int i = 0; i < n; i++) sum += nextLong();
        System.out.println(sum);
    }
}

All three programs above print 4000000015 for the input 5, then the numbers 10 -3 4000000000 7 1, on any layout of lines and spaces.

Reader Speed Notes
Scanner Slowest Regex per token; fine for tiny inputs
BufferedReader + StringTokenizer Fast The everyday choice
StreamTokenizer Faster Numbers are double; no exact long beyond 2^53
DataInputStream byte reader Fastest More code; numbers only unless you extend it

Python

The details are in Python for Contests; here they are side by side, with a measurement. The in-page runner has no real standard input, so the demo replaces sys.stdin with an in-memory file that behaves the same way.

Python Three ways to read 200,000 numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For output, collect the answers in a list, join them with "\n" and write once with sys.stdout.write or a single print. Calling print 10^6 times is slow in every language for the same reason endl is slow in C++.

A token reader that works like the Java one

When a problem mixes numbers and words on irregular lines, reading all tokens and handing them out one by one is the safest approach. This class is the Python twin of Java's FastReader:

Python A token reader for irregular input

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reading from a file on your own computer

On a judge, standard input comes from the test data. At home you want to run your program on a long sample without retyping it. There are two ways, and the first one needs no change to the program.

Redirection

Save the sample as input.txt. Then ask the shell to feed it to your program, and optionally to save the output:

TEXT
./solution < input.txt
./solution < input.txt > output.txt
python solution.py < input.txt
java Main < input.txt

< connects a file to standard input and > connects standard output to a file. This works the same on Linux, macOS and Windows (in cmd and PowerShell, use Get-Content input.txt | python solution.py if < is not accepted). Your program contains no file names, so the same file is accepted by the judge. Prefer this approach.

Opening files inside the program

Some old contests and some environments, including a few contest sites, require reading input.txt and writing output.txt from the program. The code is short in each language.

C++
#include <cstdio>
#include <iostream>

int main() {
    freopen("input.txt", "r", stdin);    // from now on, stdin is the file
    freopen("output.txt", "w", stdout);  // and stdout writes to the file
    int a, b;
    std::cin >> a >> b;
    std::cout << a + b << '\n';
}

freopen re-points stdin and stdout, so the rest of the program does not change. With C++ streams you can also use ifstream fin("input.txt") and ofstream fout("output.txt") and read with fin >> x.

Java
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("input.txt"));
        PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int a = Integer.parseInt(st.nextToken());
        int b = Integer.parseInt(st.nextToken());
        out.println(a + b);
        out.close();
    }
}

In Python, open gives you a file object, and you can also replace sys.stdin and sys.stdout so that the rest of the program keeps using input() and print():

Python Reading input.txt and writing output.txt in Python

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Keep file I/O out of your submission

Two habits prevent a lot of lost submissions.

  • Guard local-only code. Put the file redirect behind a flag or a compile-time switch, and remove it before you submit. In C++, #ifdef LOCAL around the freopen lines, compiled with -DLOCAL on your machine only, is the usual trick.
  • Know what the judge expects. Most online judges use standard input and output and do not allow your program to read input.txt. A few contests and school judges require the files. The statement's "Input" section says which, so read it before you code.

Choosing quickly

  • C++: ios::sync_with_stdio(false); cin.tie(nullptr);, '\n', and a custom fread reader only if needed.
  • Java: BufferedReader with StringTokenizer, PrintWriter, and flush() at the end.
  • Python: sys.stdin.read().split() or input = sys.stdin.readline, and one joined write for output.
  • Test with redirection from a file, never with typing; submit with plain standard I/O.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does ios::sync_with_stdio(false) do in C++?

    Without the link to C's stdio, the streams buffer independently and are much faster. The price is that you must not mix cin with scanf (or cout with printf).
  2. 2. Why is std::endl slow in a loop that prints a million lines?

    Each flush forces the buffered text to be written out. Printing '\n' lets the buffer fill and be written in large blocks.
  3. 3. A Java solution that uses PrintWriter runs but the judge shows empty output. What is the most likely cause?

    A buffered writer keeps the text in memory until it is flushed. Without flush() or close() the data may never reach standard output.
  4. 4. What is the main limit of StreamTokenizer for contest input?

    StreamTokenizer parses numbers into a double field (nval), which holds integers exactly only up to 2^53.
  5. 5. How should you feed a long sample to your program on your own machine?

    Redirection keeps the program on plain standard input, so the same code works on the judge.

Summary

  • Slow I/O, not the algorithm, causes many Time Limit Exceeded verdicts; fast idioms read big blocks and write once.
  • C++: sync_with_stdio(false) and cin.tie(nullptr), print '\n', never mix streams with scanf; use getchar_unlocked or an fread buffer for giant inputs.
  • Java: avoid Scanner; use BufferedReader with StringTokenizer (or a byte reader) and a PrintWriter that you flush.
  • Python: read everything with sys.stdin.read().split() or rebind input to sys.stdin.readline, and join the output into one write.
  • Test with shell redirection (< input.txt); open files in the program only when the contest requires it, and guard that code so it never reaches the wrong judge.

Next lesson: Edge Cases and Common Pitfalls.