Noble_Mushtak's blog

By Noble_Mushtak, history, 5 years ago, In English

Usually, I hear people saying that std::cin and std::cout is a relatively slow form of input while scanf and printf is a faster form of input which should be sufficient for all problems. However, sometimes I wonder if there are any problems where scanf and printf would be too slow.

For example, look at the problem 1181D — Irrigation. This problem asks us to read in 500,000 numbers, each of which could be as large as 500,000, and then asks us to read in 500,000 more numbers, each of which could be as large as $$$10^{18}$$$. Clearly, this is a lot of input/output, so users should use scanf and printf over std::cin and std::cout. However, even when I used scanf and printf, my solution ran in 2355 ms, barely under the time limit of 2.5 seconds. On the other hand, when I wrote my code using an I/O library I wrote myself, which uses fread and fwrite to read/write 32768 bytes at a time, my improved solution ran in 592 ms, almost 4 times faster than the original solution.

Since my code barely ran under time using scanf and printf, I wonder if there are any CodeForces problems where using scanf and printf will inevitably lead to a Time Limit Exceeded error and users must use some other I/O method like I did. Has anyone else encountered a problem where scanf and printf just weren't quite fast enough? Moreover, has anyone else built a custom I/O library for competitive programming like I did? If anyone has knows how to do input/output faster than fread and fwrite, I would love to hear about it.

  • Vote: I like it
  • +5
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Noble_Mushtak (previous revision, new revision, compare).

»
5 years ago, # |
  Vote: I like it +12 Vote: I do not like it

don't submit using slow C11

your same code in C++17

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I had I/O issues with https://codeforces.net/problemset/problem/321/E. My solution was to read line by line, but I think that shouldn't be necessary.

»
5 years ago, # |
  Vote: I like it +3 Vote: I do not like it
»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

When testing the problems, I don't think they use your I/O library or any advanced one so I guess you should always be fine with printf/scanf

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I suppose you might know that C++ IO with sync turned off (ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);) is actually faster than using scanf/printf. I never had some serious issues using this. As [user:tuwuna]have mentioned, compiler with higher version of language standard actually makes IO significantly faster. So generally in high enough version, those IO must not be an issue.

(input speed) (output speed)
Here is a blog in Korean online judge community where various input/output methods for various languages are tested by reading and writing 10m integers. Text itself is written in Korean, but it seems understandable with google translation.

There is a mmap() which is faster than fread, but I don't think it is very useful, since it is not really that much faster. Especially, you should consider that there will almost never be 10m input. JAVA or other languages will SUFFER since they don't even have method comparable to fread. As you see, JAVA takes more than half a second reading 10m input.