PrevNext

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 MM (0M10\le M\le 1) and NN (1N1061\le N\le 10^6), followed by a sequence of NN non-negative integers each less than 109+710^9+7.

  • If M=0M=0, output the sum of the input sequence modulo 109+710^9+7.
  • If M=1M=1, output the sum of each prefix of the input sequence modulo 109+710^9+7.

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).

StatusSourceProblem NameDifficultyTags
PlatinumInsane

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)

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 cin from cout. Tied streams ensure that one stream is flushed automatically before each I/O operation on the other stream.

By default cin is tied to cout to ensure a sensible user interaction. For example:

std::cout << "Enter name:";
std::cin >> name;

If cin and cout are 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 (because cout is buffered by default, output is flushed/displayed on the console only on demand or when the buffer is full).

So if you untie cin from cout, you must make sure to flush cout manually every time you want to display something before expecting input on cin.

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

StatusSourceProblem NameDifficultyTags
CFNormal

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!

PrevNext