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 :)

4
  • What are you comparing? The time that it takes to compile an application from source, or the runtime performance? Dec 31, 2008 at 13:46
  • 2
    Could be more interesting with some examples. Some measurements with timings + number of lines of code.
    – Kb.
    Dec 31, 2008 at 13:50
  • I am comparing the compilation time, not the run time.
    – Lawand
    Dec 31, 2008 at 13:56
  • See stackoverflow.com/questions/318398/… Not sure if this one should be closed as a duplicate question, so just posting the link for now. :)
    – jalf
    Dec 31, 2008 at 13:59

11 Answers 11

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.

1
  • 1
    Surely C# and Java are only doing half the job they compile to ByteCode and stop leaving the rest of the job for when the code is run. C++ is doing everything in one go. Other factors such as C++ using huge text based header files for every compile doesn't help either. Jun 11, 2013 at 13:52
6

I think the most difficult part is not the need to compile the header files (unless they are really big, but you can use precompiled headers in that case). The worst part is always the fact that C++'s grammar is too wildly context-sensitive. Despite the fact I like C++, I feel sorry for anybody who has to write a C++ parser.

1
  • 8
    Don't worry - that's about 10-20 people in the world :)
    – MSalters
    Jan 2, 2009 at 14:11
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 during compilation they must be processed.

Support in Java for generics is more limited in many ways. At the end, for example, there is only one Vector class binary, and whenever the compiler sees Vector in java what it does is generating type checking code before delegating to the real Vector implementation (that stores plain Object) and that is not generic. The compiler does provide the type warranties, but does not compile Vector for each type.

In C# it is, once again, different. C# support for generics is more complex than that of Java, and at the end generic classes are different than plain classes but anyway they get compiled only once as the binary format has all required information.

3

Because they do something quite different, C++ compiler produces optimized native code whereas C#, VB .Net and Java compiler produce an intermidiate language than when you first execute the application is turned into native code, and that is why you get slow loading of application in Java etc. the first time you execute the application.

The C++ compiler has to do the full optimization where the JITed languages optimize when you execute the application.

Someone would argue that you have to measure C++ compile time = Java compile time + time for JITing the first time you load the application if you want to be correct, but i don't think that would be right and fair because you are comparing native languages to JITed, or else oranges to apples.

1
  • C# compile + ngen = C++ compile. ngen is fast.
    – erikkallen
    Dec 31, 2008 at 14:36
2

The C++ compiler must repeatedly compile all the header files and there are lots of them, so this is one thing that slows it down.

1
2

One of the more time consuming tasks when compiling is code optimization.

Javac does very little optimization on the code when doing the compilation. Optimization is instead done by the JVM when running the application.

A C/C++ needs to be optimized when compiling since optimization of compiled machine code is hard.

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

2

If you think javac is fast try Jikes.... (see http://jikes.sourceforge.net/) It is a Java Compiler written in C++. Unfortunately they haven't kept up with the latest Java Compiler specs but if you want to see fast this is it.

Tony

0
1

I think part of it is the complexity of the languages. C++ is incredibly mutable, with the ability to override pretty much any operator or piece of syntax (like overriding the () operator). This means the compiler has to do a lot more work just to determine what operations to actually run, even for simple things. Java and C# don't have this issue, as the syntax is fixed, and they're generally much simpler to parse.

1

It's a bit difficult comparing bytecode languages like java with natively compiled languages like C++. A better comparison is Delphi vs C++, where Delphi is much faster to compile. Since this has nothing to do with optimization or byte code, it must be due to differences in language syntax and the relative performance of includes vs. modules/units.

0

Is Java compiler fast?

The Java to class translation shall be blindingly fast since it is just a glorified zip with some syntax checking so to be fair if compared to a real compiler that is doing optimization and object code generation the "translation" from Java to class is trivial.

Did a comparison with fairly small program "hello world" and-and compare to GCC (C/C++/Ada) and found that javac was 30 times slower, and it got even worse in runtime?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.