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

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

I had to use recursion+strings for a problem in a contest and I stumbled upon this irregularity.

Sample Code 'A' with strings:

Using input=5
Results:
G++ 11 5.1.0 = 7987 ms, 2044 KB
G++ 14 6.2.0 = 889 ms, 1856 KB
------------------------------------

Sample Code 'B' without strings:

Using input=6
Results:
G++ 11 5.1.0 = 3026 ms, 2012 KB
G++ 14 6.2.0 = 2995 ms, 1868 KB

To test it yourself, head over to http://codeforces.net/problemset/customtest

Why is there such a huge difference when strings are used?

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

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

Do you know the difference between void f(string s) and void f(string &s)?

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

    Yes. Are you trying to say that G++14 optimises it automatically?

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

      I think it doesn`t. Because following code takes 171ms in both C++11 and C++14. Probably there is another way to optimize this.

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

      I'm trying to say that you can use this & and the code becomes significantly faster, as no new instances of strings will be created. Negative votes show that people don't understand it.

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

        I guess negative votes are because your answer has anything common with the question "why is there such a huge time difference".

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

    The compiler can't change the declaration of f (it is illegal to go from string s to string &s).

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

      I dont think so. You cant use std::move for s because you are calling go(...) 20 times with same arguments. std::move spoils string, it will be a problem to use it here.

      Or maybe I misunderstood what are you talking about?

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

        Yes, I guess you are right. There has to be another trick done by the compiler.

        L.E.: This isn't only for strings. The optimization is done for vectors (and I guess that for all containers) too.

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

In g++ version 5.4.1 both programs (-std=c++11 or -std=c++14) have the same speed. So it probably is just an optimization the compiler introduced in version 5.2-4.

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

It might be that on codeforces, G++14 code runs on a faster processor. Optimisations at compiler level don't make sense.

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

I believe it's due to a different compiler version, not 11 or 14 standard.

I have tested it on my computer on g++ 6.3 (both g++11 and g++14 ran around 1s) and g++ 4.9 (both ran around 1.3s). On ideone g++ and g++14 (both from version 6.3) run in the same time while g++ (but version 4.3.2) is slower (output is clock ticks).

In all these runs there was no difference between runtimes of g++, g++11 and g++14, while there were differences between versions. I didn't get to test 5.1 though.