tags:

views:

913

answers:

4

I notice on the same machine, it takes C# much less time than C++ to compile. Why?

NOTE1: I have not done any scientific benchmark.

NOTE2: Before anyone says this isn't programming related, I am implementing a parser, I am finding what I can do from the get go to increase compile speed.

NOTE3: I have a similar question http://stackoverflow.com/questions/588884/why-do-compilations-take-so-long. This question is asking on the specific difference from C/C++ to C#. It's obvious a simple language would be quicker to compile than a complex language, but C and C# are both complex languages.

my takeaway: 1) C/C++ is SLOW from preprocessor and headers. 2) alot of headers causes a lot more data to parse. especially when each file can use the preprocessor can change code 3) C# defer some compilation to program startup 4) IL instructions are simple, machine is not

+9  A: 

Take a look at this post: http://stackoverflow.com/questions/318398/why-does-c-compilation-take-so-long#318440

Stormenet
this is surely a duplicate... though the pointer to the excellent answer is good :)
ShuggyCoUk
A: 

Because C++ compiles to machine code, while C# to byte code. Have you noticed the lag when you first start your .NET program. It is when the byte code gets JITed (compiled to machine code).

Darin Dimitrov
There is a lot more to this question than that though.
Ed Swangren
Delphi compiles to machine code too, and it's faster than C#, which doesn't.
Arafangion
the major problem is the source, not the target.
peterchen
A: 

I think it's probably because of the AMOUNT of parsing it has to do, rather than the speed of the parser itself.

C++ normally uses the C preprocessor, which pulls in lots of include files (as others have suggested, and the other question contains many answers like it). This bloats out the amount of code to parse.

So if you're comparing them for purposes of writing a parser... learn that you should not have .h-style include files :)

MarkR
A: 

There are two separate issues to consider - the number of phases of processing, and the complexity of targeting.

A typical C++ compilation involves a number of phases (though these may be run concurrently) where the Preprocessor handles directives and macros, then the C++ compiler itself processes the resulting code. It's pretty common for the preprocessor to generate output that is significantly bigger, code that all needs to be parsed and processed by the actual compiler.

Also, keep in mind that the C++ compiler will be targeting x86 or x64 machine language - handling all optimization up front, and attempting to make best use of hardware that isn't really optimized at OO style development.

In contrast, the C# compiler is targeting Microsoft Intermediate Language (MSIL), a higher level machine-code-like platform that was designed to be used for OO development. Many of the constructs provided by C# map directly into IL instructions, making compilation really easy. A fair chunck of optimization and other activity is deferred until startup of the actual program, at which point it's optimized for the exact available machine.

Bevan