Aden_blizzard's blog

By Aden_blizzard, history, 4 years ago, In English

i was practicing this question https://www.codechef.com/problems/COPR16G/ and i found that endl gives TLE but \n doesnot why? is it because endl is flushing after giving a newline?

  • Vote: I like it
  • -14
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +19 Vote: I do not like it

cout << endl; is the same as cout << "\n" << flush;. Flushing a buffer is very slow, because if you output just a small string, it's something like 1 flush per 10 characters, and if you let cout do the buffering automatically, it's something like 1 flush per several thousand characters. That's why the runtime is so different.

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

    ooh thanks i didnt faced it till now so i thought maybe i went mad :D

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

      Just curious, what gave you the motivation to change all endl to "\n"? Or did you try all other stuff but still got tle?

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

define endl "\n"

you can use this in your template

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

    And then you might get ILE in an Iterative Problem =)

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

      well then you can just comment the define line

      the advantage of this is that you don't have to switch between endl and '\n' all the time, you can just comfortably use endl and comment the define line when necessary

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

        And then you might get TLE in an Iterative Problem =)

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

          mou, nii, that's not fun >:(

          you have to flush the buffer when printing outputs for interactive problems anyway, so solutions with endl should pass, it won't make sense otherwise