3

why they use this before#include<bits/stdc++.h> mainly I've been using #include ... Now I'm seeing these lines on a cpp program so I became curious.

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("Ofast")//Comment optimisations for interactive problems (use endl)
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Sourav9063
  • 292
  • 2
  • 7
  • 3
    only they know why they use it. For what it does you could look at the [manual](https://gcc.gnu.org/onlinedocs/gcc/Function-Specific-Option-Pragmas.html) – 463035818_is_not_an_ai May 12 '20 at 18:45
  • 3
    If you know exactly what compiler is being used, the operating system, the CPU, and sundry other details you can perform wonders. But if someone tries to run the code on a different machine... the results are kind of a combination of sobbing and tears and LOL. – user4581301 May 12 '20 at 18:45
  • 6
    competitive programming is one of the worst ways to "learn" to programming. You see many bad habits and things you should not do. – t.niese May 12 '20 at 18:47
  • 6
    By the way, `#include` [can be a bit of a mistake](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). Don't include it, or anything else in the bits folder, directly. – user4581301 May 12 '20 at 18:48
  • 3
    Prefer [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to _competitive programming_. – Ron May 12 '20 at 18:48
  • 3
    Competitive programming is a load of old bollocks. Get a programming job instead. – Bathsheba May 12 '20 at 18:48
  • 7
    *"Hey, my program runs fast, but the code is pure slop and no one will be able to understand it."* -- That is what a lot of what competitive programming is about. And to be honest, I bet that a well-written, understandable program may be just as fast, or maybe even faster than the "competitive" version. – PaulMcKenzie May 12 '20 at 18:48
  • 2
    More seriously, I prefer candidates who can write maintainable code rather than fast code. Good compilers write fast code. You merely program the intention. Think economically: computers are getting faster and faster which means that slow code tends to fix itself over time anyway. Unmaintainable code gives you a headache, and burdens you with an economic cost. And programmers cost more than computers. – Bathsheba May 12 '20 at 18:51
  • 1
    The old Dilbert gag about [crap code being the holy grail](https://dilbert.com/strip/1994-06-10) is becoming less common. Most organizations prefer to have and use code that is readable, and maintainable because it's cheaper to fix and you have fewer surprises at the security audit, if the code lasts long enough to get audited. – user4581301 May 12 '20 at 18:56
  • 1
    Make the code faster. p.s. Do these competitions allow constexpr so you can do it all at compile time? – QuentinUK May 12 '20 at 19:00

1 Answers1

4

These are indications to the compiler to change its behaviour as if you had passed the equivalent command line flags. For programming competitions, you often submit source code rather than a binary - it's then built and tested using a system you don't control (and can't set the command line on). Putting these settings in as #pragma lines lets you control settings you might not otherwise be able to do in the competition environment.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 2
    perhaps worth mentioning that it is not just any compiler but gcc in particular. The code will fail to compile with most others – 463035818_is_not_an_ai May 12 '20 at 18:50
  • 2
    It'll probably compile, but the pragmas won't do anything. Compilers are allowed to silently discard unknown pragmas. And if you NEED that pragma for something the "silent" part really sucks. – user4581301 May 12 '20 at 18:51
  • 2
    Playing with `#pragma` is sometimes playing with fire, yes. But the whole point of them is to give compiler specific hints, so.... – Carl Norum May 12 '20 at 18:54