2

I have installed G++ 4.8 with the C++11 standard.

Searching around the internet I found some flags that apparently should decrease compile time considerably. I have ended with this, it compiles fine but not sure if its entirely correct.

-std=c++0x -Ofast -mtune=cortex-a7 -mfpu=neon-vfpv4 -mfloat-abi=hard 

I am no G++/C++ expert and was wondering if there is anything I should change with that to get better compile performance on the Pi 2 or if there are any caveats I have not run into yet by using these arguments.

Piotr Kula
  • 17,307
  • 6
  • 65
  • 104
  • 1
    Those flags are unlikely to make any difference to compile times or anything else, since the compiler is already optimized for the native platform (meaning they're pointless)-- something you can test for yourself, at least WRT compile times. They tend to be propagated by OCD and/or cargo cult types, I think. They probably won't do any harm though. -Ofast may make a difference but it comes with some caveats -- see man gcc. Generally in development you want -O0 to ensure that values are not optimized out when debugging. The default is I think -O2. – goldilocks Jun 08 '15 at 22:14
  • 1
    An exception to this might be if you are using an ARMv6 userland (i.e., with an ARMv6 g++), which is possible on the Pi 2 but would be slower than a dedicated ARMv7 userland. However, since the libc is then ARMv6, I'm not sure how much advantage you'll really get, presuming the compiler is capable of it in the first place. That's a more complex question qualified with "when using raspbian on the pi 2", since there is no ARMv7 raspbian -- but if you are new to C++ you are wasting your time being concerned with these kinds of subtleties. – goldilocks Jun 08 '15 at 22:35
  • @goldilocks thanks. Problem is the sample I am compiling takes 2 minutes but it's hardly 100 lines of code. I suspect it's c++11 that causes this. – Piotr Kula Jun 09 '15 at 07:00
  • There's no magic bullet here. I compile C++11 stuff on a Pi 2 and B/B+'s all the time, and it is slow; as much as I can I work on it elsewhere. If you have multiple parts to compile w/ make, the 2 is obviously faster. But 2 minutes for 100 lines is a bit much. Can you paste it anywhere? – goldilocks Jun 09 '15 at 10:11
  • Thanks Goldilocks. At the moment I cant paste my code. It is based on this code though, and as it is, that code takes ages to compile... and its not complex. http://www.linux-projects.org/downloads/examples/uv4l-overlay.cpp – Piotr Kula Jun 09 '15 at 10:21
  • 1
    I'm guessing there's some serious templating in those boost headers which would account for this -- i.e., you're really compiling a lot more than just those 100 lines. That is an unfortunate thing about C++ and generics. – goldilocks Jun 09 '15 at 10:33
  • Yea... I suspected that. I forgot about the boost part.. I wonder if I can move away from that. Not sure why the author uses boost there(or what it does actually.. only to make a JSON file I suppose)... or even C++11(Better syntax and all but is it really necessary?) – Piotr Kula Jun 09 '15 at 10:35
  • PS is C++0x differnt to c++11 ? I use the c++0x to compile but also used c++11 before. Both compiled fine. – Piotr Kula Jun 09 '15 at 10:49
  • 1
    You don't have to be using the boost stuff there, no. I'm not seeing the significance of the ptree thing at all, part of it seems to be about writing json data out to a temp file ??? Those boost headers pull in a bunch more boost headers. WRT using standard C++11, I don't find it slower to compile than old code. c++0x is the same as c++11 for compilers that support the latter; some < 4.7 gcc's only support the former, which was basically the 2011 standard before it was finalized. – goldilocks Jun 09 '15 at 10:51
  • Thank you so much for you comments. I will remove the boost headers and see what happens. I dont need to write any json in my code any way and will post the difference when I get back to that tonight. – Piotr Kula Jun 09 '15 at 10:55
  • I couldn't wait :) And yes! What a difference. It compiled in about 2 seconds now after removing boost! Fantastic!!!! – Piotr Kula Jun 09 '15 at 11:12
  • 1
    Well then http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378 :P – goldilocks Jun 09 '15 at 11:14
  • Lesson learnt. Avoid XY'ing :] I also removed all the g++ compiler flags as not needed. – Piotr Kula Jun 09 '15 at 11:16

1 Answers1

1

On the Pi2 one easy enhancement is to add -j4 to make use of the four cores.


As goldilocks points out the above won't help with a single compilation. -j is an option to make and will only be useful if the make can be broken into multiple jobs.

joan
  • 71,024
  • 5
  • 73
  • 106
  • Hi Joan. Please excuse my ignorance.. but will that -j4 apply to the G++ compiler? I am just learning about C++ thanks to the Pi. And not sure how make and g++-4.8 relate to each other. I mean, I don't have to use make if I just use g++-4.8 source.cpp but make executes g++-4.8 source.cpp for me. – Piotr Kula Jun 08 '15 at 19:53
  • 1
    @ppumkin I believe so. They are all part of the gcc (GNU Compiler Collection) suite and a lot of the options will be common to all languages. – joan Jun 08 '15 at 20:18
  • -j4 is a make option and make is not part of GCC. Make, which is about building multiple objects, can run in parallel; a C/C++ compiler, which is about building one object at a time, does not. If you run make with -j, it will potentially spawn multiple concurrent instances of the compiler. Using -j4 with make is certainly more useful than bothering with most of the "optimizing" switches in ppumkin's question if make serves a useful purpose (because multiple discrete compilation units are involved). – goldilocks Jun 08 '15 at 22:20
  • As I suspected. I only got 1 file to compile. It takes way too long to compile as it's not very big or complex. But as I mentioned it may be c++11 std making it take long to compile. – Piotr Kula Jun 09 '15 at 07:03
  • @ppumkin Yes, sorry about that, my mistake. Is it a first time through thing? I have noticed that the first compile after boot is a lot longer than subsequent ones (caching I guess). – joan Jun 09 '15 at 07:28
  • Its alight. I was not completely specific with my question... but all that led to, two important questions being answered in one go :) (I am using visualdgb and compile very frequently... that's why its annoying me cause 100 lines of code takes ~2 minutes to compile on the Pi before it executes.. arggh - as metioned before it must be the c++11 std cause plain c/c++ compile instantly anyway. ) – Piotr Kula Jun 09 '15 at 07:32