The USACO Instructions Page briefly mentions some ways of speeding up I/O:
For some of the more advanced problems with larger input sizes, competitors may benefit from using fast input/output, to more easily pass within the time limit. For C++ users, you may want to add "ios_base::sync_with_stdio(false); cin.tie(0);" to the top of your main method if you are using cin/cout. For Java users, you may want to use BufferedReader instead of Scanner.
What do these do and how much of a difference do these actually make? We'll use the following task to benchmark I/O speed:
Example Task
The input consists of two integers () and (), followed by a sequence of non-negative integers each less than .
- If , output the sum of the input sequence modulo .
- If , output the sum of each prefix of the input sequence modulo .
Sample Input 1:
1 6 1 2 3 4 5 1000000000
Sample Output 1:
1 3 6 10 15 8
Sample Input 2:
0 6 1 2 3 4 5 1000000000
Sample Output 2:
8
Randomly generating test data results in input and output files each ~10MB large. It is possible to see input files this large (the 11th input file for Robotic Cow Herd is ~10.3MB large), though not output files (the largest we know of is due to Minimum Cost Paths, which has an output file ~2.8MB large).
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| Platinum | Insane | |||||
Standard I/O
Slow
Some simple methods of I/O don't come close to running under the time limit:
input + print (18.9s)
Fast
Replacing input with sys.stdin.readline results in a huge speedup.
readline + print (2.9s)
Using sys.stdin.readline and sys.stdout.write is a bit faster:
readline + write (2.4s)
File I/O
Pretty similar to standard I/O.
readline + write (2.4s)
C++
Additional Notes
| Resources | |||||
|---|---|---|---|---|---|
| CF | timing various I/O methods | ||||
ios::sync_with_stdio(false)
| Resources | |||||
|---|---|---|---|---|---|
| CPP | documentation | ||||
| SO | |||||
From the second resource:
This disables the synchronization between the C and C++ standard streams. By default, all standard streams are synchronized, which in practice allows you to mix C- and C++-style I/O and get sensible and expected results. If you disable the synchronization, then C++ streams are allowed to have their own independent buffers, which makes mixing C- and C++-style I/O an adventure.
cin.tie(nullptr)
| Resources | |||||
|---|---|---|---|---|---|
| CPP | documentation | ||||
| SO | |||||
From the second resource:
This unties
cinfromcout. Tied streams ensure that one stream is flushed automatically before each I/O operation on the other stream.By default
cinis tied tocoutto ensure a sensible user interaction. For example:std::cout << "Enter name:";std::cin >> name;If
cinandcoutare tied, you can expect the output to be flushed (i.e., visible on the console) before the program prompts input from the user. If you untie the streams, the program might block waiting for the user to enter their name but the "Enter name" message is not yet visible (becausecoutis buffered by default, output is flushed/displayed on the console only on demand or when the buffer is full).So if you untie
cinfromcout, you must make sure to flushcoutmanually every time you want to display something before expecting input oncin.
Warning: cout.tie(nullptr)
You may see some competitive programmers including this line. This doesn't
actually do anything since cout isn't tied to anything. See
this post for
details.
Java
Python
Problems
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| CF | Normal | |||||
Module Progress:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!