Why C # is so much faster than C + + compilation

Source: Internet
Author: User
Tags php compiler

Go I do not understand, the following in C + + and C # comparison to explain why C + + compilation Slow and C # compilation fast.

The compilation of C and C + + files follows several main steps:

    1. Handling chores such as line continuation processing ("\")
    2. Lexical analysis, parsing out tokens to
    3. preprocessing, macro expansion, processing #include, and then repeat the next steps for the files contained in the #include.
    4. Re-lexical analysis
    5. Syntax parsing generates abstract syntax tree AST
    6. Semantic analysis
    7. Optimizing Generated code


Steps for C #:

    1. Handling chores such as line continuation processing ("\")
    2. Lexical analysis, parsing out tokens to
    3. Syntax parsing generates abstract syntax tree AST
    4. Semantic analysis
    5. Optimizing Generated code

First of all, visually, C # is less than C + + in the compilation phase, which is one reason.

Another reason is the language of the compilation speed and syntax is very large, high-level language compilation is usually through lexical analysis and grammar analysis and semantic analysis----optimization of the generation of code several steps, if the language is not ambiguous syntax, the compilation of each stage is independent, then the compilation speed naturally fast.

If not, there are grammatical ambiguities, such as the ">>" of the C + + nested template and the ">>" of the bitwise operation, and when the compiler encounters a nested template ">>", it has to be placed first, and then take a few more steps to determine whether it is the angle bracket or the shift of the template, Nature is slower than a language without grammatical ambiguity.

For example, C + + call template function instantiation can omit <> let the compiler to deduce the most appropriate overload, C # generic method call can also omit <>. Syntax elements such as this make it possible for the compiler to determine in the parsing phase whether a function call is a function template instantiation (a generic method call for C #) or a normal function call, but not until the semantic analysis phase can be determined, so at this point C + + and C # In fact, all choose to sacrifice the compilation speed to reduce the number of keystrokes to contribute to human and slightly improve the readability of the code.

Another aspect is generated by C and C + + compile-time code generation problems: header files and macro definitions will generate code at compile time, macro expansion and introduction of the header file equivalent to hard insert a large piece of text, so that the compiler needs to re-parse from the lexical analysis, the template is not directly inserted text, but the processing is also very cumbersome, It consumes a lot of compile time. If C + + is not a header file but some form of compilation module, multiple source files (compilation unit) reference the same module then the compiler only needs to parse the module generation AST reuse, the compilation speed can also improve a lot.

C/C + + stupid text insertion form of the #include, the assembly compiler completely eliminates the compilation of header files, you refer to the direct supply of the type and method of God horse metadata, the compiler directly to use the good.

Finally, because C # does not have code-generation tools such as macros and templates, generics are also run-time and further speed up compilation.

----------
Off Topic
about compiler bootstrap and compile speed relationship is not big, currently (VS2013) C # compiler should be written in C + +, compile fast, but Mono compiler is C # write, feel not slow, at least there is no difference in order of magnitude. Now even m$ new C # compiler Rosylin is also completely written in C #, but also very modular, performance is not bad oh.

Said so much, in fact I mean C + + and C # than PHP compiler speed is far worse, PHP is the best language!

Why C # is so much faster than C + + compilation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.