Tor Didriksen <Tor.Didriksen@stripped> writes:
> On Mon, 29 Jun 2009 12:55:45 +0200, Kristian Nielsen
> <knielsen@stripped> wrote:
>> Mats, could you describe what the benefits are of using forward
>> of structs/classes in place of #including the definition?
> Please see
> for an explanation.
> If a class 'Bar' is implemented in terms of another class 'Foo', or
> uses it's interface in some way,
> it will typically store a pointer to it (in its private section).
> By forward declaring the class
> class Foo;
> rather than
> #include "foo.h"
> you can often drastically cut down on the number of header files included.
Ok, so to reduce compile time.
> The savings in compile time can be quite substantial.
> I remember doing this on a project sometime in the 90's cutting down
> compile/build time from a day (yes 24 hours) to about one hour.
If you expect this kind of improvements on today's computers you may be
disappointed. CPUs are _much_ faster, C++ compilation can be parallelised
across multiple cores (as opposed to eg. linking), and I suspect compilers
these days spend proportionally less time on parsing #include compared to code
Just for fun, I did some quick benchmarking on building the latest mysql 5.1
bzr tree on a cheap 2-year old quad-core. First, compiling one file in sql/
vs. compiling all:
`touch sql/mysqld.cc ; make -j5` -> 11.35 seconds, 121% CPU.
`touch sql/*.cc ; make -j5` -> 120.87 seconds, 352% CPU.
So that is a factor of only 10 between compiling one and all C++ sources. Of
course that is still something if we can get down to only one (or a few) files
#including something vs. all doing so.
Second, I tried adding #include <elf.h> (random 100k bytes, 2.5k lines file)
at the start of every .cc file in sql/, and timing `make -j5` after
Without #include <elf.h>: 5 min 14 sec
With #include <elf.h>: 5 min 17 sec
So not much difference there (may even be less than the statistical noise).
Anyway, thanks for the explanation,