Optimisation options

Hi all !
I’ve found the libraries availables for C++ in the FAQ but not the optimisation options.
Does this means there is no optimisation enabled ? Not even -O2 ?

6 Likes

Indeend, there are no optimisation flags at the moment, but we might think to include it in next release.

Thanks for the suggestion

1 Like

For solo games/puzzles it doesn’t matter but for multiplayer contests it can make a big difference,
so if it’s included in the next release that’s great.
Thanks for your answer !

1 Like

Hi,
I can’t find anything about libraries nor compiler flags in FAQ. Is it still there?

Hi,

  • Thanks for the link. I think that it should be much more visible that it is now (it’s tiny text in the bottom right corner of some pages).
  • -O2 is absolutely a must-have for C++.
  • ‑lpthread, ‑ldl, ‑lcrypt - are those really needed?
  • Why not C++14? It’s supported by g++ 4.9
4 Likes

I think the change for C++14 is a great a idea.

I do practice C++ on Codingame and there have been a lot of times when I wanted to do things in the C++14 way, but couldn’t do it. (I doubt that I’m the only one who had to make this tradeoff)

Really, there’s no -O2 ?
I think it should really be included:

  • without -O2, you’re wasting resources which means CodingGame is spending more money than it should :slight_smile:

  • to have proper performances on some puzzles like nintendo 256 bits, without -O2, you have to do yourself some optimizations that should be handled by the compiler. It means messing with a clean code to make it fast in ways that are not advisable in the real world (it’s teaching wrong lessons).

Example of optimizations that not having -O2 may force you to do:

  • use an integer to loop on array instead of using iterators
  • make the caller create result objects of functions to simulate return value optimization (or worse, copy paste the code of the function in your code)
  • create additional variables for all intermediary results of trivial functions that would otherwise be inline (e.g: vector size)
  • etc …

Clearly these are not good practices …

[EDIT] Looking back on it, what I said is mostly rubbish :slight_smile: First no puzzle require such optimization. Moreover, comments in answer to my post are right, code is only executed a few times so the -O2 is probably more expansive than what is saved while running (especially if most run fail at the first test).

1 Like

I would really love to see C++ running at real world speeds on CG.

Locally my AIs run so much faster just compiling with O3.

1 Like

I guess for Codingame it’s a tradeoff. Longer compile time and shorter execution time against shorter compile time and longer execution time. Without some numbers I couldn’t say which is more important.

There are many pros and cons as to adding optimization flags.

However, what you are saying in your examples are not completely true.
One big point is that after profiling an application to optimize it, you need to know how you can optimize the code.

As for creating additional variable for trivial functions being bad manner, I do not agree with you. When you need to use that function multiple time, calling it once and storing the result in a variable helps making a more understandable code. In addition, if you modify how you get that data, you only need to fix it in one place, instead of multiple places.

I don’t know how easy it would be for you to change it but I find the current state of the environment promotes poor C++ code. I have encountered quite a few examples while coding on the site where C++ code becomes slower when using better practices, which I don’t think is normal. Here are a few examples I can remember and/or have seen recently:

-pow(verylongAnnoyingFormula,2) is much slower than verylongAnnoyingFormula*verylongAnnoyingFormula
-vector performs poorly compared to C style arrays (is it doing runtime bounds checks?)
-C++11 range for loops slower than C style int loop
-Classes can be significantly slower than having a bunch of ints and floats

I personally find it frustrating that I have to benchmark the most meaningless details of my code because it is being compiled god knows how (my interpretation at least).

What you can do is add the line:
#pragma GCC optimize "-O3"
at the top of your code (above the includes).

I’ve tested it today with my solution of the Skynet Strikes Back puzzle and it gives 2-4X improvement on my code. Note that I prefer using the STL heavily, so that’s probably where inlining has the most effect. Your performance increase may be different.

5 Likes

Thank you for sharing this.

However I did a few tests and it seems to me like “O3” and “O1” give me the same performance. I’m going to guess that the compiler is actually going into “Og” which are optimisations compatible with debug mode -g.

It would be REALLY nice if we could turn off debug mode and have O3, at least for multiplayer games. It is really frustrating to lose, for example, 50% performance by using a vector class instead of silly x and y parameters. I really feel like it promotes bad practice.

It might also be really nice if we could get compiler warnings, and not just errors, by ticking a checkbox for example. For example I wrote constexpr int a{0.3}; by accident and didn’t get the warning I would have gotten compiling with warnings.

or change -Og to -O3 when submitting your code

I guess some compilation options have changed since I tried the pragma I previously mentioned in this topic. Just in case someone lands in this topic, the pragma should be:
#pragma GCC optimize "O3,omit-frame-pointer,inline"

2 Likes

i just tested it here
http://gcc.godbolt.org/#compilers:!((compiler:g492,options:’-O0+-g+-std%3Dc%2B%2B11’,source:’%23include+<algorithm> %23include+<vector> int+foo(int+a,+int+b)+{+return+std::max(a,+b)%3B+} int+bar(int+a,+int+b,+int+c,+int+d)+{ ++auto+A+%3D+std::vector<int>{a,b,c,d}%3B ++std::sort(A.begin(),+A.end())%3B%0A++return+A%5B0%5D%3B%0A%7D%0A’)),filterAsm:(colouriseAsm:!t,commentOnly:!t,directives:!t,labels:!t),version:3

it generates not the same ASM code as with -O3 in comp options - for example, STL still generates a big bunch of not optimized code like ~vector() ~VectorBase() ~allocator(), when with -O3 it entirely removed Vector class and used just operators new() and delete()

1 Like