What are the differences between macros, functions, and macro functions?

Source: Internet
Author: User

FirstDifferences between macros and functions:
1. macros are used to replace simple strings (note that strings are replaced rather than other types of parameters), while function parameters are transmitted with data types, it can be of various types.
2. Macro parameter replacement is directly processed without computation, while function calling transfers the value of real parameters to the form parameter. Since it is a value, it is naturally calculated.
3. the macro is executed before compilation, that is, the macro name is replaced with the macro name and then compiled. The function is called only after compilation and execution. therefore, macros take up Compilation Time, while functions take up execution time.
4. macro parameters do not occupy memory space, because they only replace strings, while parameter transmission during function calling is information transmission between specific variables, and form parameters are used as partial variables of the function, the memory usage is obvious.
5. function calling requires a certain amount of time and space overhead, because the system needs to retain the field when calling the function, and then transfer it to the called function for execution. After the call, the system returns the main function, at this time, the site will be restored. These operations are obviously not available in the macro.
Now let's look at the inline function:
The so-called "inline function" is to embed a very simple function "into the code that calls his program, just to avoid the 5th points mentioned above, the objective is to save the time and space overhead of the original function call. however, it must be noted that as an inline function, the function body must be very simple and cannot contain complex structures such as loops, conditions, and selections. Otherwise, it cannot be used as an inline function. In fact, even if you do not specify a function as an inline function, some compilation systems automatically process simple functions as inline functions. For complex functions, even if you specify it as an inline function, the system does not care.

 

Differences between functions and macro functionsThat is, macro functions occupy a lot of space, while functions take up time. You need to know that function calling uses the system stack to store data. If the compiler has the stack check option, generally, some Assembly statements are embedded in the function header to check the current stack. At the same time, the CPU also needs to save and restore the current site during function calling to perform stack pressure and elastic stack operations, therefore, it takes some CPU time to call a function.

This problem does not exist in macro functions. Macro functions are only embedded into the current program as pre-written code and do not generate function calls. Therefore, they only occupy space. This phenomenon is particularly prominent when the same macro function is frequently called.

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.