0

I am having issues with compile times in my code base. I have found the culprit to be the two lines shown below. With the below code, it takes about 1m 40s to compile.

bool successful=true;
successful&=TreeDebug().run();
successful&=FileDebug().run();
time g++ -static-libgcc -static-libstdc++ -Wall -I . -o bin/mainDebug.exe debug/*.hpp debug/*.cpp src/*.hpp src/*.cpp

real    1m41.439s
user    0m0.000s
sys     0m0.015s

Separating the creation of the debug objects and the calling of the run method on that object reduces the compile time to 0m 15s.

bool successful=true;
TreeDebug treeDebug;
FileDebug fileDebug;
successful&=treeDebug.run();
successful&=fileDebug.run();
time g++ -static-libgcc -static-libstdc++ -Wall -I . -o bin/mainDebug.exe debug/*.hpp debug/*.cpp src/*.hpp src/*.cpp

real    0m14.159s
user    0m0.000s
sys     0m0.015s

What is happening that would increase the time to compile so drastically?

The Tree class is a template class, but the last result proves that it is not the template that is causing the issue.

  • This code is not equivalent. Did you try to cast the objects in the second sample to rvalues? – Dmitry Kuzminov Jun 02 '20 at 02:40
  • How is it not equivalent? I guess I'm not understanding something fundamental here. – JackCarmichael Jun 02 '20 at 02:44
  • Looks like this whole thing [is making demons fly out of your nose](http://www.catb.org/jargon/html/N/nasal-demons.html). `&=` is a bitwise operation. A bitwise operation on a `bool` value makes no sense to me, whatsoever. I can't blame a C++ compiler for getting confused over this. I suppose this is not ill-formed, but it's still making the compiler blow a gasket. – Sam Varshavchik Jun 02 '20 at 02:45
  • I was thinking that it was shorthand for this: ```successful=(successful && treeDebug.run());``` Am I wrong about that too? Interesting link btw, never heard of that before. – JackCarmichael Jun 02 '20 at 02:47
  • It is possible that in the first sample the const versions of your `run()` method are called while in the second regular non-const are. – Dmitry Kuzminov Jun 02 '20 at 02:49
  • 1
    You don't really see the difference between `&&` and `&`? One is a logical and operator. The other is a bitwise and operator. – Sam Varshavchik Jun 02 '20 at 02:50
  • @DmitryKuzminov I only have non-const methods defined. – JackCarmichael Jun 02 '20 at 02:51
  • What if you leave just a single class `TreeDebug`? What if you leave the other? – Dmitry Kuzminov Jun 02 '20 at 02:52
  • @SamVarshavchik I understand the difference between ```&``` and ```&&```. Would ```succesfull&&=treeDebug.run();``` be the proper syntax? [This](https://stackoverflow.com/questions/6577504/is-there-any-difference-between-and-with-bools) seems to say what I'm doing is correct? – JackCarmichael Jun 02 '20 at 02:53
  • @DmitryKuzminov Removing the ```TreeDebug``` class had minimal effects on compile time (shaved off a couple seconds out of the original minute fourty). – JackCarmichael Jun 02 '20 at 02:55
  • 1
    Try without putting the `.hpp` files in the compilation command? – M.M Jun 02 '20 at 02:59
  • @M.M That did it. Compilation is now down even further to **11s** even with the original code, what does removing the ```.hpp``` files from the command do?? – JackCarmichael Jun 02 '20 at 03:03
  • @SamVarshavchik [This](https://stackoverflow.com/questions/2488406/why-doesnt-c-have-or-for-booleans) proves your point I think. What I'm doing works, but only because both operands are ```bool```'s. But, the link also describes how its not good coding practice, thanks for the tip. – JackCarmichael Jun 02 '20 at 03:08
  • @JackCarmichael gcc builds precompiled headers if you give it .h or .hpp files. Which is a slow procss and generally you would have one (or none) pch for the whole project , not one for every hpp file – M.M Jun 02 '20 at 04:57

0 Answers0