Alex Rivera | Logout

What makes the Java compiler so fast?

Asked 2008-12-31T13:39:26.133
12

I was wondering about what makes the primary Java compiler (javac by sun) so fast at compilation?

..as well as the C# .NET compiler from Microsoft.

I am comparing them with C++ compilers (such as G++), so maybe my question should have been, what makes C++ compilers so slow :)

Edit
Report

3 Answers

16

That question was nicely answered in this one: Why does C++ compilation take so long? (as jalf pointed out in the comments section)

Basically it's the missing modules concept of C++, and the aggressive optimization done by the compiler.

answered 2008-12-31T14:04:15.350
6

There are a couple of things that make the C++ compiler slower than those of Java/C#. The grammar is much more complex, generic programming support is much more powerfull in C++, but at the same time it is more expensive to compile. Inclusion of files work in a different way than importing modules.

Inclussion of header files

First, whenever you include a file in C++ the contents of the file (.h usually) are injected in the current compilation unit (include guards avoid reinjecting the same header twice), and this is transitive. That is, if you include header a.h, that in turns includes b.h, your compilation unit will include all code in a.h and all code in b.h.

Java (or C#, I will talk about Java, but they are similar in this) don't have include files, they depend on the binaries from the compilation of the used classes. This means that whenever you compile a.java that uses an object B defined in b.java, it just checks the binary b.class, it does not need to go deeper to check the dependencies of B, so it can cut the process earlier (with just one level of checking).

At the same time, including files only includes the language definitions, and processing it requires time. When the Java/C# compiler reads a binary it has the same information but already processed by the compilation step that generated it.

So at the end, in C/C++ more files are included and at the same time processing of those includes is more expensive than processing of binary modules.

Templates

Templates are special in their own way. They can be precompiled, but they are usually not (for a good set of reasons). This means that in all compilation units that use std::vector the whole set of vector methods used (unused template methods don't get compiled) is processed and the binary code generated by the compiler. At a later step, during linking, redundant definitions of the same method will get dropped, but du

answered 2008-12-31T14:12:50.403
2

You got it right in your last sentence: it's not java or C# that's fast to compile, it's C++ that is exceptionally slow to compile, due to its complex grammar and features, most importantly templates

answered 2008-12-31T14:03:46.287

Your Answer