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

Автор absi2011, история, 6 лет назад, По-английски

I used cin and got TLE in Feb 15th.

Then I test the scanf and got Accepted.

I found it interesting that different C++ get different verdict with cin.

Several times of MS C++ because I found interesting of it TLE on different test.

My Submissions on 932D - Tree with submissions 40405924 (Code is same, Language is different)

MS C++ Time limit exceeded on test 80

GNU C++17 Accepted 1356 ms

GNU C++14 Accepted 1326 ms

GNU C++11 Time limit exceeded on test 81

GNU C++ Accepted 1902 ms

GNU C++17 Diagnostics Time limit exceeded on test 11

MS C++ Time limit exceeded on test 81

MS C++(scanf) Accepted 421 ms

MS C++(Submit on Feb 15th) Time limit exceeded on test 82

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

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

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

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

I have done a "test" on that and it showed that if you gonna use cin for big input (even with ios::sync_with_stdio(0) and cin.tie(0)) C++ 17 and C++14 will give better results than C++11 and C++ "on some conditions". Although I don't know the reason behind that. And I got downvotes when I made a blog about it.

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

    I don't understand why people thinks that things like speed of some functions depends on C++ language version, while it actually should depend on compiler used. Ofcourse, specific compiler with different language versions is actually different compilers, but it has nothing to do with "C++17 gives better results" — maybe, only for this specific compiler, so results on your computer and on CF differs a lot.
    Also, different IO gives different results on different types of input/output data, so "research" on one specific test is actually useless.

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

      1#: I actually did that on Codeforces. Not in my device.

      2#: I did it with integers which is the most common data type in problems. I will try the same with strings later.

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

        What about many small numbers? Or less, but bigger numbers? Or mixed data? Or boolean data? What about reading int versus reading long long. What about non-integer numbers? "Research" called "research" for a reason.

        Also, I was talking generally, not about CF. And it seems pointless to me to test such things on CF, cause it's such a rare case when IO speed actually matters here. cin/cout never failed me.

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

          I am actually cin/cout user. And it did not fail me neither. But absi2011 has a point to discuss. You can call what I did what you want. I did very small "test" which is reading 200000 positive and negative integers about between -1e9 and 1e9. C++17 and C++14 were two times faster than C++11 and C++. And we are here in Codeforces. I don't personally care if another online judge gives different results. My test was about Codeforces and not anything else. And you are absolutely right about the point of different input types. What I did was the most common input type from my experience so far. You can analyze the results as you want. (which is better than me). Saying that the test was useless is not helping anybody here.

          • »
            »
            »
            »
            »
            »
            6 лет назад, # ^ |
            Rev. 2   Проголосовать: нравится -18 Проголосовать: не нравится

            By the way, three people from top-5 on codeforces use scanf / printf: Um_nik, jqdai0815, Syloviaely.

            I tested input / output (int, long long, double, strings) on MinGW 8.1 (enabled on codeforces) with:

            std::ios_base::sync_with_stdio(false); 
            std::cin.tie(0); std::cout.tie(0); std::cerr.tie(0);
            

            iostreams faster then stdio only on 32-bit integers.

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

              In other news, two people from top-2 on codeforces use cin / cout.

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

              tourist uses cin/cout btw. but all of them use GNU C++14. And fateice uses scanf/printf also. I am glad to see the exact results if you can.

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

                GNU C++14 not a compiler, it's only flag -std=c++14 in compiler command, maybe something else. The speed of input is influenced by the program code of the compiler, it is different for each compiler, but the behavior of the code is subject to the standard. What is faster on windows with I/O: cygwin or mingw? Scanf/printf, iostreams in cygwin, or scanf/printf, iostreams in mingw?

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

                  Ralsei said "Ofcourse, specific compiler with different language versions is actually different compilers". And did I say that GNU C++14 is a compiler?

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

                I am glad to see the exact results if you can.

                I did it

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

Code for Input/Output 4.000.000 numbers with MinGW x64.

MinGW Results

Code for Input/Output 4.000.000 numbers with Cygwin x64. Results:

Cygwin Results

UPD: Code for my first approach of implementation fast input / output classes using fread / fwrite

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

    Why double is so slow with ofstream/printf but fast in fwrite?

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

      Compiler developers develop stable, thread-safe I / O, they are limited by the standard, plus they must consider the exponential form too. I have not looked at the source code in the compilers, but I think that they call too many template functions to provide the above guarantees to the user. Here I wrote my own input / output function for double and it's fast, maybe someone can input / output double faster. I know that MikeMirzayanov or his team have own input / output library for checkers on github

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

The problem was using MS C++ in the first place.

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

So which compiler should i use to fasten my Code?