Блог пользователя Aden_blizzard

Автор Aden_blizzard, история, 3 года назад, По-английски

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?

  • Проголосовать: нравится
  • -14
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится +19 Проголосовать: не нравится

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.

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +2 Проголосовать: не нравится

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

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

define endl "\n"

you can use this in your template

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится +8 Проголосовать: не нравится

      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

      • »
        »
        »
        »
        3 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

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

        • »
          »
          »
          »
          »
          3 года назад, # ^ |
            Проголосовать: нравится +8 Проголосовать: не нравится

          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