When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

thekushalghosh's blog

By thekushalghosh, 3 years ago, In English

Hello CodeForces Community,

Input / Output in Python can be sometimes time taking in cases when the input is huge or we have to output many number of lines, or a huge number of arrays(lists) line after line.

I have come across many questions on CodeForces where the style of taking input and printing makes your code quite faster.

For Input :-

Normally, the input is taken from STDIN in the form of String using input(). And this STDIN is provided in the Judge's file. So why not try reading the input directly from the Judge's file using the Operating system(os) module, and input / output(io) module. This reading can be done in the form of bytes.

The code for that would be :-

import io,os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

or a native method (which can be used for not too large inputs) :-

import sys
input = sys.stdin.readline

Now for Output :-

Instead of throwing the output to the STDOUT, we can try writing to the Judge's sytem file. The code for that would be to use sys.stdout.write instead of print. But remember you can only output strings using this, so convert the output to string using str or map.

Examples :-

For printing an integer, instead of

print(n)

Use :-

sys.stdout.write(str(n) + "\n")

For printing a list of integers, instead of

print(*list)

Use :-

sys.stdout.write(" ".join(map(str,list)) + "\n")

Now Program examples On CodeForces, where this was useful :-

Question 1
TLE Solution
AC Solution

Question 2
Earlier Solution
Faster Solution

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

| Write comment?
»
3 years ago, # |
Rev. 2   Vote: I like it +6 Vote: I do not like it

I want to add something. When using BytesIO method of input, reading strings does not work normally like the STDIN method. Rather it returns a bytecoded string (I say bytecoded string, I am not sure what it's really called).

Suppose you input a string somestring as follows, and then print it

s=input()
print(s)

It will print b'somestring' ,rather than somestring. If you want to print somestring you will have to decode the bytecoded string, and do something like this:

s=input().decode()
print(s)

So, the overall code will be:

import io,os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
s=input().decode()
print(s)

Remember this only happens in the case of strings. Integers behave normally.

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

Thanks a lot! I was getting TLE of 2 sec problem. After implementing this i made it in 1091 ms. It is very very helpful when you have to take input or give output in a loop/several times.

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

I used input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline and it just returns empty string without waiting for input.

What am I doing wrong?

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    whole code please?

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Using it in shell for example:

      >>> import os, io
      >>> input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
      >>> input()
      b''
      >>>
      
      • »
        »
        »
        »
        2 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Looks like this code needs to read input from I/O redirection. Use it like:

        # test.py
        import os, io
        input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
        
        s = input().decode().rstrip("\r\n")
        print(s)
        

        and $ python3 test.py < input.txt

        • »
          »
          »
          »
          »
          2 years ago, # ^ |
            Vote: I like it -10 Vote: I do not like it

          Thanks.

          And, do you know why 147370723 didn't work? The previous submission (147342596) got TL, and I just used this method of input for list "a" in the next one.

          • »
            »
            »
            »
            »
            »
            2 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            I just modified your solution and tested a bit, it can be TLE again (no runtime error) but still not passing the tests.

            You can refer to 147473106 which is what I got after modifying your implementation. I use set instead of list to indicate whether a row/col is colored or not.

            • »
              »
              »
              »
              »
              »
              »
              2 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              I took a look at your solution and updated mine (147482726); yours got AC and mine TL again. (I wonder why)

              But finally I could use this method of input. As it seems, it should not be mixed with other methods.

              And the big question for me is that why io.BytesIO(os.read(0, os.fstat(0).st_size)).readline cant be used in place of the input variable it is assigned to. (it would return empty string)

              • »
                »
                »
                »
                »
                »
                »
                »
                22 months ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                i am getting an error while using this line of code:

                input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  22 months ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  What error?

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

Which language is the best for competitive programming.

  • »
    »
    2 years ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    Well, each language has strength and weakness.

    Python is very convenient, but the speed is very slow and you will easily get TLE. C++ is mainstream language and very fast, but it is easy to fail system test, like forget long long int and some variable numbers may exceed 2147483647.

    Java, go, swift also have pro and cons.

    The best strategy is that you should master more than one language, one fast and one slow, like c++ and python. For loose constraint problem, use python to save time, and for tight constraint problem, use C++ to get passed.

»
20 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Hey! I have used above methods but they do not work for me , even if they are working on my local machine .Please check these submissions 164067953 and 164066429 and help me to recitify the mistakes. Thank you

  • »
    »
    20 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The solutions have Wrong Answer because your logic is incorrect....

    Fast Input / Output will only work if your logic is correct (obviously) :p

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Thank you so much. It's what I was needing.

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    why I used this way for input but it returned empty string?

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Can confirm that changing the code from print to sys.stdout.write turned a TLE (https://codeforces.com/contest/1726/submission/208423821) to AC (https://codeforces.com/contest/1726/submission/208424281)

Note that changing the input to sys.stdin.readline didn't make a difference for this particular problem (ie getting AC using the plain built-in input function). This must be because this particular problem is write heavy (reads two numbers per test case, prints out up to 10**5 per testcase). I will just use sys.stdin and sys.stdout always in the future, to cover both read heavy and write heavy problems.

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

I am getting this error message from codeforce:"'str' does not support the buffer interface" from this code "input_str = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline().rstrip("\r\n")"

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

It's been 3 years since this was posted.

I'd say newer Python versions work well with print(' '.join(map(str, A))) and open(0).read().split().

n, k, *x = [*map(int, open(0).read().split())]
# if x should be a list of tuples
c = [(a,b,c) for a,b,c in zip(*[iter(x)]*3)]

The io.BytesIO approach might be better for string based inputs (to not have utf8 on by default). I couldn't measure the difference at all with sys.stdout.write or io.BytesIO on int inputs.

The general idea is to read the whole input once and process it in memory (as is the case in the longer example above). Similarly, calling print once is as fast as calling sys.stdout.write. print(*A) is slower than print(' '.join(map(str, A))).