### Giada's blog

By Giada, history, 5 years ago,

I encountered the following issue with C++14. It seems that it is much slower than C++ and C++11. Here the same code submitted with C++, C++11, C++14.

The issue is not related to I/O (so cin/cout slowness addressed here is not involved), since the program has only to read 2 integers and write 1 integer. Moreover the code doesn't use any exotic structure or library, but only vectors.

What is the cause of this issue? Is it a known problem of the compiler?

• +75

| Write comment?
 » 5 years ago, # |   +23 Yes, I also noted that on different website (https://acmp.ru, unfortunately the only alternative there is MS VS++ 2008).I don't know what's going on here, but this is exact reason why I don't switch from C++11's compiler to C++14.
 » 5 years ago, # | ← Rev. 2 →   +52 I'm using C++14 in codeforces since they added it.After reading your blog I tested one of my codes with C++11 and the difference was huge : 3306ms vs 622ms.Thank you for your warning, I won't use it anymore :).By the way, What are C++14's advantages?
•  » » 5 years ago, # ^ | ← Rev. 2 →   +26 By the way, What are C++14's advantages? C++14 adds mostly nothing codeforces-useful: from the top of my head, there's decltype, auto return type, generic lambdas, std::make_unique, compile-time integer sequence templates.
•  » » 5 years ago, # ^ |   +23 C++11 isn't always better either. Run this code in custom input using G++ 11/14:#include using namespace std; int n; void go(string s,int x){ if(x>n) return; string p="1"; s+=p; for(int i=0;i<20;i++){ go(s,x+1); } } int main(){ n=5; string s="1"; for(int i=0;i<20;i++){ go(s,1); } } Results:G++ 11 5.1.0 = 7987 ms, 2044 KBG++ 14 6.2.0 = 889 ms, 1856 KB Thread discussing this speed difference can be found here.
•  » » 5 years ago, # ^ | ← Rev. 2 →   +8
 » 5 years ago, # | ← Rev. 2 →   0 For another problem (requires large input), C++11 29193595 608 ms in test case 40 C++14 29193764 342 ms in test case 40 ¯\_(ツ)_/¯
 » 5 years ago, # |   +22 halyavin, we need one more investigation.
 » 5 years ago, # |   +18 My solution that yesterday worked 0.7s with C++14, now works 1.6s. So I think it's just a temporary problem on server side and will be fixed soon.
•  » » 5 years ago, # ^ |   +18 I'm not sure this is the reason, indeed the issue was present also yesterday as you can see in this two identical submissions: C++11 29174185 405 ms in test case 8 C++14 29174169 > 1000 ms in test case 8
•  » » » 5 years ago, # ^ |   +1 Oh yes, In my case it's just because new tests were added to educational round problems, but old solutions were not rejudged for some reason.So now I agree, it seems to be some problem in G++ 6.2 optimizations.
 » 5 years ago, # |   +18 I think it's incorrect to say that it's caused by difference between C++14 and C++11 but rather by different g++ versions 6.2.0 and 5.1.0. C++14/11 compilation mode could be enabled in both but I don't really think it could trigger vastly different code generation (it mostly just enabling/disabling of features).
 » 5 years ago, # |   -15 Remember that on Windows we work with MinGW, g++ port to Windows. And MinGW has a lot of issues, including performance issues. So i don't know any case for original g++, where the same code compiled with C++11 is much faster than code compiled with C++14. Clang and GCC compiler are very good compilers (MSVC isn't).
•  » » 5 years ago, # ^ |   -10 Count blogs about bugs in GCC. Then count blogs about bugs in MSVC. Compare two numbers.
•  » » » 5 years ago, # ^ |   +62 It is impossible to find a bug in MSVC, because no one uses MSVC.
•  » » » » 5 years ago, # ^ |   0
•  » » » » » 5 years ago, # ^ | ← Rev. 3 →   +31 But my soltion, which is one of the best by time in that problem, works 3 times slower with MSVC.upd. I found that this is because of the slow input cin >> string. With gets it works as fast as G++ and Clang.
 » 5 years ago, # |   +33 Looks like a MinGW-specific issue. I tested this code using 3 different compilers (gcc 5.4.1, gcc 6.3.0 and clang 3.8.0) and 3 different standards for each of them (c++03, c++11 and c++14) under ubuntu 16.04. There's no performance difference between all 9 combinations.
 » 5 years ago, # |   +65 So it looks like hotspot is in calling the copy constructor of std::vector in line vector at_is = is;In general this line is not very performant (depending on number of calls) because vector allocated size doesn't get reused and thus vector always calls new every time we get to this line. Also we can observe that changing vector lifetime to static and thus reusing its capacity as in the following lines: static vector at_is; at_is = is; Changes time on this test quite drastically, leading to ~155ms times for both G++s which seems to prove my original point.I looked on assembly of this part for both versions of g++ in question on Compiler Explorer and it seems to result in exactly the same new + memmove calls. So apparently it may be MinGW specific issue related to memory allocation.
 » 5 years ago, # |   +5 I have a different story, C++11 is slower (almost twice RT):C++11: 342msC++14: 186msC++17: 186ms
•  » » 5 years ago, # ^ |   0 here's another similar case (x 2.5 slower)is something wrong with containers in C++11?C++11: 623msC++14: 264msC++17: 249msSubmission with C++17 is fastest everytime!