 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Yves Dufournaud Guest
|
Posted: Sun Apr 24, 2005 3:46 pm Post subject: Why C++ is so slow to compile ? |
|
|
Hello All,
This is a general question :
from my experience C/C++ are very slow to compile
compared to Java or C#.
I have for example never see a C compiler that match Java compile time speed
for example
(even if it's faster to compile than C++).
Used of precompiled header can boost things a bit, but figures that come
to my mind and that are comming from real projects are for example :
3000 java file in order of 1 min (this is overpriced in fact)
and on the same computer
for about the same order of number of cpp files in many hours.
I have got no personnals stats on C#, but I suspect that they match Java.
I'am wondering what cause this slowliness : does it comes from the language
itself that impose some different architecture on the compiler ?
If so, which parts ?
Yves
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
M Jared Finder Guest
|
Posted: Sun Apr 24, 2005 8:24 pm Post subject: Re: Why C++ is so slow to compile ? |
|
|
Yves Dufournaud wrote:
| Quote: | Hello All,
This is a general question :
from my experience C/C++ are very slow to compile
compared to Java or C#.
I have for example never see a C compiler that match Java compile time speed
for example
(even if it's faster to compile than C++).
Used of precompiled header can boost things a bit, but figures that come
to my mind and that are comming from real projects are for example :
3000 java file in order of 1 min (this is overpriced in fact)
and on the same computer
for about the same order of number of cpp files in many hours.
I have got no personnals stats on C#, but I suspect that they match Java.
I'am wondering what cause this slowliness : does it comes from the language
itself that impose some different architecture on the compiler ?
If so, which parts ?
|
There are a two major things that make C++ slower to compile than Java
or C#.
1. C++ is a much more complicated language than C# or Java. This makes
it take much longer to parse.
2. Preprocessing file inclusion combined with pre-processing's
conditional compilation means that each #include of a header file may
generate differnt code. This means that to be correct, each header file
must be reprocessed for each time it is #include'd, for each source
file. Combining this with the more complicated parser makes things even
worse.
-- MJF
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Sun Apr 24, 2005 8:57 pm Post subject: Re: Why C++ is so slow to compile ? |
|
|
Yves Dufournaud wrote:
| Quote: | This is a general question : from my experience C/C++ are very
slow to compile compared to Java or C#.
I have for example never see a C compiler that match Java
compile time speed for example (even if it's faster to compile
than C++).
Used of precompiled header can boost things a bit, but figures
that come to my mind and that are comming from real projects
are for example : 3000 java file in order of 1 min (this is
overpriced in fact) and on the same computer for about the
same order of number of cpp files in many hours. I have got
no personnals stats on C#, but I suspect that they match Java.
I'am wondering what cause this slowliness : does it comes from
the language itself that impose some different architecture on
the compiler ? If so, which parts ?
|
I'm not 100% sure of what you're really comparing, but certainly
much of the difference could be explained by the fact that a C++
compiler usually compiles directly to machine code (including
some degree of optimization), whereas as Java compilers normally
compile to a relatively simple intermediate representation, and
only really compile to machine code at run-time. Note the
start-up times for Java programs, compared to C++, for example.
Of course, I'm supposing that you are speaking of complete
builds. I find that often, when I'm just trying to do a syntax
check on a single module, C++ actually compiles faster. The
Java compiler will automatically compile all of the imported
packages, if the .class files for them aren't available, whereas
the C++ compiler is quite happy with just a header.
--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Walter Guest
|
Posted: Sun Apr 24, 2005 8:58 pm Post subject: Re: Why C++ is so slow to compile ? |
|
|
"Yves Dufournaud" <yves.dufournaud (AT) free (DOT) fr> wrote
| Quote: | I'am wondering what cause this slowliness : does it comes from the
language
itself that impose some different architecture on the compiler ?
If so, which parts ?
|
Some of it is due to inefficient compiler implementations, see
http://biolpc22.york.ac.uk/wx/wxhatch/wxMSW_Compiler_choice.html
A factor of 2 or 3 slowdown comes from needing to support the preprocessor
and the multiple phases of translation.
But the biggest culprit is the #include system. More and more of the
semantics of a C++ program are being shoved into the .h files (macros,
constants, templates, inlines), and the natural tendency of .cpp files is to
#include every .h file in the project. Those .h files wind up #include'ing
most of the system .h files.
The end result is that to compile each .cpp source file, the compiler has to
process an incredible amount of source text. Multiply that by the number of
source files in the project, and a build is going to take a long time.
Languages with true modules, such as the D programming language, Java, C#,
etc., don't have this problem because each module is compiled only once, and
then symbolically referenced (rather than textually #include'd).
-Walter
www.digitalmars.com C, C++, D compilers
"code of the nerds"
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Graeme Prentice Guest
|
Posted: Mon Apr 25, 2005 1:12 pm Post subject: Re: Why C++ is so slow to compile ? |
|
|
On 24 Apr 2005 16:58:41 -0400, Walter wrote:
| Quote: |
"Yves Dufournaud" <yves.dufournaud (AT) free (DOT) fr> wrote in message
news:426b858e$0$29863$636a15ce (AT) news (DOT) free.fr...
I'am wondering what cause this slowliness : does it comes from the
language
itself that impose some different architecture on the compiler ?
If so, which parts ?
Some of it is due to inefficient compiler implementations, see
http://biolpc22.york.ac.uk/wx/wxhatch/wxMSW_Compiler_choice.html
A factor of 2 or 3 slowdown comes from needing to support the preprocessor
and the multiple phases of translation.
But the biggest culprit is the #include system. More and more of the
semantics of a C++ program are being shoved into the .h files (macros,
constants, templates, inlines), and the natural tendency of .cpp files is to
#include every .h file in the project. Those .h files wind up #include'ing
most of the system .h files.
|
When you say "natural tendency" do you mean because of header files
#including other header files or do you mean because the #include list
in a .cpp includes more .h files than it needs to. If the latter, is it
considered a bad thing to have more files included in the .cpp file than
needed, or even to include all .h files, if compile times aren't an
issue?
| Quote: |
The end result is that to compile each .cpp source file, the compiler has to
process an incredible amount of source text. Multiply that by the number of
source files in the project, and a build is going to take a long time.
Languages with true modules, such as the D programming language, Java, C#,
etc., don't have this problem because each module is compiled only once, and
then symbolically referenced (rather than textually #include'd).
|
How do you mean symbolically referenced? Are Java, C#, Delphi or D
intermediate files "re-parsed" when they're referenced from another
module or do the intermediate files contain an "index" that allows fast
lookup on symbols?
Graeme
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dmitry Mityugov Guest
|
Posted: Mon Apr 25, 2005 1:21 pm Post subject: Re: Why C++ is so slow to compile ? |
|
|
Yves Dufournaud wrote:
| Quote: | Hello All,
This is a general question :
from my experience C/C++ are very slow to compile
compared to Java or C#.
I have for example never see a C compiler that match Java compile time
speed
for example
(even if it's faster to compile than C++).
Used of precompiled header can boost things a bit, but figures that come
to my mind and that are comming from real projects are for example :
3000 java file in order of 1 min (this is overpriced in fact)
and on the same computer
for about the same order of number of cpp files in many hours.
I have got no personnals stats on C#, but I suspect that they match Java.
I'am wondering what cause this slowliness : does it comes from the
language
itself that impose some different architecture on the compiler ?
If so, which parts ?
|
I believe this is because meanings of many C++ keywords heavily depend on
the context in which they're using. For example, keyword 'static' may denote
at least 5 (*five*) different things, depending on the context:
- a static variable defined outside of a function
- a static variable defined inside of a function
- a static function
- a static method of a class
- a static data member of a class
Dmitry
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Mon Apr 25, 2005 1:21 pm Post subject: Re: Why C++ is so slow to compile ? |
|
|
[email]walter (AT) digitalmars (DOT) nospamm.com[/email] (Walter) wrote (abridged):
| Quote: | But the biggest culprit is the #include system. More and more of the
semantics of a C++ program are being shoved into the .h files (macros,
constants, templates, inlines), and the natural tendency of .cpp files
is to #include every .h file in the project. Those .h files wind up
#include'ing most of the system .h files.
[...]
Languages with true modules, such as the D programming language, Java,
C#, etc., don't have this problem because each module is compiled
only once, and then symbolically referenced (rather than
textually #include'd).
|
Agreed. Also, I don't know about D and C#, but in Java user-defined types
are always used by reference. This means they can avoid some transitive
dependencies. In C++ we have the choice. If you write, for example:
class Rect {
Point topLeft;
Point bottomRight;
};
then you will need to #include "Point.h" because the compiler needs to
know the size of Point. In Java you can only write the equivalent of:
class Rect {
Point *topLeft;
Point *bottomRight;
};
with indirection, and for this a forward-declaration is sufficient even in
C++.
-- Dave Harris, Nottingham, UK.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Apr 25, 2005 1:27 pm Post subject: Re: Why C++ is so slow to compile ? |
|
|
Walter wrote:
| Quote: | "Yves Dufournaud" <yves.dufournaud (AT) free (DOT) fr> wrote in message
news:426b858e$0$29863$636a15ce (AT) news (DOT) free.fr...
I'am wondering what cause this slowliness : does it comes
from the language itself that impose some different
architecture on the compiler ? If so, which parts ?
Some of it is due to inefficient compiler implementations, see
http://biolpc22.york.ac.uk/wx/wxhatch/wxMSW_Compiler_choice.html
A factor of 2 or 3 slowdown comes from needing to support the
preprocessor and the multiple phases of translation.
But the biggest culprit is the #include system. More and more
of the semantics of a C++ program are being shoved into the .h
files (macros, constants, templates, inlines), and the natural
tendency of .cpp files is to #include every .h file in the
project. Those .h files wind up #include'ing most of the
system .h files.
|
If you have "a natural tendency of .cpp to #include every .h
file in the project", I'd say that you have a serious design
problem. In practice, the expansion due to include files isn't
negligible, but it is far from being the problem you seem to
suggest (unless someone is making exagerated use of templates).
| Quote: | The end result is that to compile each .cpp source file, the
compiler has to process an incredible amount of source text.
Multiply that by the number of source files in the project,
and a build is going to take a long time.
|
For the system headers, I think most compilers today support
some form of precompiled headers. For the project specific
headers, you really do need some sort of dependancy management.
But not just because of compile times.
| Quote: | Languages with true modules, such as the D programming
language, Java, C#, etc., don't have this problem because each
module is compiled only once, and then symbolically referenced
(rather than textually #include'd).
|
It's a two edged sword. My only experience is with Java, and I
found it a pain that 1) the compiler had to compile all of the
modules I "included", including the implementation of the
module (which meant I couldn't compile until there were
implementations for everything I used), and 2) changing a single
character in the implementation of a member function triggered
the recompilation of all code which used the module.
All in all, the C++ situation is pretty bad, but Java managed
too do worse. (What is really needed is something along the
lines of Ada.)
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
E. Rosten Guest
|
Posted: Mon Apr 25, 2005 11:05 pm Post subject: Re: Why C++ is so slow to compile ? |
|
|
On Sun, 24 Apr 2005 11:46:45 -0400, Yves Dufournaud wrote:
| Quote: | Hello All,
This is a general question :
from my experience C/C++ are very slow to compile
compared to Java or C#.
I have for example never see a C compiler that match Java compile time speed
for example
|
Since you're talking about C compilers as well here, have you tried TCC?
It is a very fast compiler (~ 10x faster than gcc -O0), but it doesn't do
much optimization. The benchmrks say that on a P4 2.4GHz, it does about
900,000 lines of code per second.
You would have a hard job making a really fast C++ compiler,
since template meta-programming is turing complete, so you could always
write a program which takes for ever to compile.
-Ed
--
(You can't go wrong with psycho-rats.) (er258)(@)(eng.cam)(.ac.uk)
/d{def}def/f{/Times findfont s scalefont setfont}d/s{10}d/r{roll}d f 5/m
{moveto}d -1 r 230 350 m 0 1 179{1 index show 88 rotate 4 mul 0 rmoveto}
for /s 15 d f pop 240 420 m 0 1 3 { 4 2 1 r sub -1 r show } for showpage
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Rob Guest
|
Posted: Mon Apr 25, 2005 11:10 pm Post subject: Re: Why C++ is so slow to compile ? |
|
|
Yves Dufournaud wrote:
| Quote: | Hello All,
This is a general question :
from my experience C/C++ are very slow to compile
compared to Java or C#.
I have for example never see a C compiler that match Java compile
time speed for example
(even if it's faster to compile than C++).
Used of precompiled header can boost things a bit, but figures that
come to my mind and that are comming from real projects are for
example : 3000 java file in order of 1 min (this is overpriced in
fact) and on the same computer
for about the same order of number of cpp files in many hours.
I have got no personnals stats on C#, but I suspect that they match
Java.
I'am wondering what cause this slowliness : does it comes from the
language itself that impose some different architecture on the
compiler ? If so, which parts ?
|
There are a few contributors I can think of.
1) C (and even more so, C++) is a complex language, so takes more time
to parse.
2) The way Java works also means that part of the compilation can
happen when the application is run, meaning there is a trade-off
between compile time and run time performance when comparing with C/C++.
3) Good quality optimisers with C and C++ are typically more
aggressive than the Java compiler, which also gives a trade-off between
compile speeds and runtime performance.
4) C and C++ work with header files to facilitate use of libraries
(which requires additional parsing) whereas the equivalent mechanism in
Java either uses precompiled mechanisms or (in some cases) does not
require the compiler to load relevant information.
5) A lot of things in Java are in the form of precompiled libraries,
but equivalent facilities in C/C++ are in the form of code that needs
to be recompiled (eg macros in C, templates in C++. Expansion of
macros and instantiation of templates is a compile time overhead
(although it is possible to reduce those overheads with selective
programming techniques).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Shannon Barber Guest
|
Posted: Tue Apr 26, 2005 9:01 am Post subject: Re: Why C++ is so slow to compile ? |
|
|
Try a Borland compiler.
Some slowness is from the more complex parsing, much of the slowness is
from the back-end optimizations, which is deferred until later with
..Net and either later or not at all with Java.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Tue Apr 26, 2005 9:20 am Post subject: Re: Why C++ is so slow to compile ? |
|
|
Graeme Prentice wrote:
| Quote: | On 24 Apr 2005 16:58:41 -0400, Walter wrote:
Languages with true modules, such as the D programming language, Java, C#,
etc., don't have this problem because each module is compiled only once, and
then symbolically referenced (rather than textually #include'd).
How do you mean symbolically referenced? Are Java, C#, Delphi or D
intermediate files "re-parsed" when they're referenced from another
module or do the intermediate files contain an "index" that allows fast
lookup on symbols?
|
In Java, a reference to Foo.bar() does not require that you have seen
Foo.java. All it requires is the presence of Foo.class on the class
path, and accessing the contents of Foo.class is generally a lot faster
than parsing the source and reconstructing the class/interface
declaration in every source file that refers to Foo. In preprocessor
based compilers, like C/C++, even with include guards, you still have
to go through the entrire text every time a file is #included.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
clemd@acm.org Guest
|
Posted: Tue Apr 26, 2005 9:30 am Post subject: Re: Why C++ is so slow to compile ? |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
| Quote: | My only experience is with Java, and I
found it a pain that 1) the compiler had to compile all of the
modules I "included", including the implementation of the
module ...
All in all, the C++ situation is pretty bad, but Java managed
to do worse.
|
That is not how Java is supposed to be used. Use an "interface" to
separate a caller from an implementation. You can write Java without
interfaces, but the consequences are, well, just as you wrote.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Walter Guest
|
Posted: Tue Apr 26, 2005 9:32 am Post subject: Re: Why C++ is so slow to compile ? |
|
|
<kanze (AT) gabi-soft (DOT) fr> wrote
| Quote: | Walter wrote:
But the biggest culprit is the #include system. More and more
of the semantics of a C++ program are being shoved into the .h
files (macros, constants, templates, inlines), and the natural
tendency of .cpp files is to #include every .h file in the
project. Those .h files wind up #include'ing most of the
system .h files.
If you have "a natural tendency of .cpp to #include every .h
file in the project", I'd say that you have a serious design
problem.
|
I've seen a *lot* of C/C++ code in the last 20 years. You can call it bad
design if you want, but the reality is that every source file eventually
winds up #include'ing, one way or another, directly or indirectly, every .h
file in the project, sometimes multiple times. I've rarely seen a project
where the programmers had the discipline to avoid this (or even considered
it a problem).
| Quote: | In practice, the expansion due to include files isn't
negligible, but it is far from being the problem you seem to
suggest (unless someone is making exagerated use of templates).
|
The only "problem" resulting from it is slow compile times. BTW, lots of
times it isn't exactly obvious in a project that this is happening. The only
way you can tell is if you have a compiler that will put out a list of what
files are actually being #include'd, or you run a utility like makedep over
the source: www.digitalmars.com/ctg/makedep.html
The problem is such that many .h files get #include'd over and over again in
the same source file. This is why the practice of wrapping .h files in
#ifdef/#define/#endif evolved, and why many compilers support #pragma once.
| Quote: | The end result is that to compile each .cpp source file, the
compiler has to process an incredible amount of source text.
Multiply that by the number of source files in the project,
and a build is going to take a long time.
For the system headers, I think most compilers today support
some form of precompiled headers.
|
Precompiled headers help a lot. But using them requires some non-standard
assumptions. And the reason precompiled headers came about is because most
of the compile time is spent munching header files. Implementing p.h. is not
easy, and it wouldn't have been done if this wasn't a big problem for
customers.
| Quote: | For the project specific
headers, you really do need some sort of dependancy management.
But not just because of compile times.
|
It's fine to suggest ways to reduce the number of files #include'd. All I'm
saying is what I see as the reality of software development in C++, and why
the compiles are slow. As a compiler vendor, I work with the reality of how
people use them. I've done a lot of profiling of C++ compilation, and I know
where the time is spent.
| Quote: | Languages with true modules, such as the D programming
language, Java, C#, etc., don't have this problem because each
module is compiled only once, and then symbolically referenced
(rather than textually #include'd).
It's a two edged sword. My only experience is with Java, and I
found it a pain that 1) the compiler had to compile all of the
modules I "included", including the implementation of the
module (which meant I couldn't compile until there were
implementations for everything I used), and 2) changing a single
character in the implementation of a member function triggered
the recompilation of all code which used the module.
|
You're right that this can happen. But if you have n java source files, the
worst case is you must compile n times, i.e. each module is only compiled
once. In C++, the worst case is n*n. Also consider that different java
compilers vary over 10:1 in compile speeds, just like C++ compilers do.
To be frank, few people consider compile speed when selecting a compiler.
I've had people insist to me that compile speed was the most important
issue, while they've standardized on using the slowest compiler on the
market <g>.
| Quote: | All in all, the C++ situation is pretty bad, but Java managed
too do worse. (What is really needed is something along the
lines of Ada.)
|
-Walter
www.digitalmars.com C, C++, D compilers
"code of the nerds"
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Tue Apr 26, 2005 9:34 am Post subject: Re: Why C++ is so slow to compile ? |
|
|
Dmitry Mityugov wrote:
| Quote: | Yves Dufournaud wrote:
This is a general question :
from my experience C/C++ are very slow to compile
compared to Java or C#.
I have for example never see a C compiler that match Java
compile time speed for example (even if it's faster to compile
than C++).
Used of precompiled header can boost things a bit, but figures
that come to my mind and that are comming from real projects
are for example : 3000 java file in order of 1 min (this is
overpriced in fact) and on the same computer for about the
same order of number of cpp files in many hours. I have got no
personnals stats on C#, but I suspect that they match Java.
I'am wondering what cause this slowliness : does it comes from
the language itself that impose some different architecture on
the compiler ? If so, which parts ?
I believe this is because meanings of many C++ keywords
heavily depend on the context in which they're using. For
example, keyword 'static' may denote at least 5 (*five*)
different things, depending on the context:
- a static variable defined outside of a function
- a static variable defined inside of a function
- a static function
- a static method of a class
- a static data member of a class
|
Which is more a problem for the user than for the compiler.
Compilers have been dealing with this sort of problem for ages.
(The - sign is overloaded for unary minus and for subtraction.
With different precedences, no less. In almost every procedural
language. Never caused any problems for the compiler.)
--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|