Oh i know what you are thinking: “Oh god plz no not that topic again they already spam us all the f*cking day on the chat with that i will kill some kittens to relax”. And you are right. But why so many people are talking about that ?
Have you tried to code in C++ on codingame ? If not, do a contest in C++ and come back here, we’ll wait for you. If you already coded in C++ on codingame, you should already noticed the following points:
- Calling a function cost you some time. Even with an inline keyword and no copy of value at all
- Using the STL is the worst idea ever. You kill your performances. Replacing
std::max
by a simple macro is 3 times faster.x*x
is something like 6 or 7 times faster thanstd:pow(x, 2)
. - Using Object Oriented Programmation is slow even when you avoid copy of values and use only inlined methods
- Local variables are slow as hell
Why is it so slow despite of all the C++ community saying everywhere on the internet that you MUST use this things ? This are good pratices, this help you to keep your code beautiful and bug-free. Only insane coders avoid this best pratices.
The reason is simple. When you work in the real world with C++, you compile with the -O3 compilation flag (or at least -O2, but i don’t know any case where -O3 is not better than -O2). But on Codingame there’s no compilation flag (not even a -O0).
When you do a contest, performances are mandatory. Ask anyone in the top 10 of a multiplayer contest and the response will always be the same. You always see C++, C and sometimes C# and Java in the top 10. That’s because theses languages just crush others in performances. The only counter example i have is the winner of Back to the code (i believe he coded in Javascript). But @pb4608 could tell you far better than me why Back to the code was a “not so great” contest.
At the moment on codingame if you want performances, you have to follow this rules:
- Only work with global variables. Forget local variables. Re-use global variables.
- Never ever ever ever EVER use any function in the
std
workspace except if you have no choice (you can’t really rewritesqrt
everytime you need it) - Don’t write functions, this is slow, use macros. But you have to know how macros works (or you’ll do something slower )
- If you want to use Object Oriented Programmation to have a better code, it will be slow as hell. Why do you think i never coded any
Vector
class in Coders Strike Back ? - You need to use
std::sort
? too bad. Just do like @pb4608, rewrite it.
If any entreprise could see my code from a codingame contest they’ll just go berserk screaming “This is the worst code i have ever seen and i code in Haskell”.
If for example you write in C#, your code will be a little slower than in C++, but you don’t have to avoid all the good pratices. You can do a standard code and have all the performances your language can have. C++ is the only language on Codingame where you just can’t code like in the real life. You have to learn specifics skills (useless outside of Codingame).
And maybe someone will give the tricks of the pragma (#pragma GCC optimize("Ofast")
or #pragma GCC optimize("O3)
). But it is NOT the same as the compilation flag. The pragma directive can’t do the same optimizations as the -O3 flag. Creating a function still cost you some time when with the compilation flag it cost nothing, STL is still slow, …
This day, we were talking about that on the webchat. And [CG]VonRickroll said that the response of Codingame was “No, we wont’ add this flag, we already said that” (not the accurate words, i can’t find it in my history). Well, i know my memory is pretty bad, but i can’t remember reading any reasons from Codingame on that.
So i ask the questions here so i think i’ll remember the responses:
- Why do we have no optimisation compilation flag at all ?
- What are the reasons i must write a horrible code just for Codingame when i can do a normal code in the real life ?
- Why can’t i use any good pratices ?
I think i already know a part of the response: “If we add the -O3 flag, C++ will be too fast and will crush all other languages”. But we can respond that:
- C++ is already crushing other languages. But you have to know how to make a “Codingame’s specificaly dirty C++ code”.
- I will be happy to have the computing time of C++ solutions reduced by something like 30% if i had the -O3 compilation flag. Writing dirty code is not one of my habits.
Thanks for reading
A Codingamer disgusted by his own code because of this