Function call overhead

Source: Internet
Author: User

I did a few experiments to learn more about the overhead of function calls.

 

Program 1-function calls without parameters:

# Include <stdio. h> <br/> void test () <br/>{< br/> return; <br/>}< br/> int main (INT argc, char * argv []) <br/>{< br/> test (); <br/> return 0; <br/>} 

Use gcc-s to get the assembly code of program 1:

. File "test. C "<br/>. text <br/>. globl test <br/>. type Test, @ function <br/> test: <br/> pushl % EBP <br/> movl % ESP, % EBP <br/> NOP <br/> popl % EBP <br/> RET <br/>. size Test ,. -Test <br/>. globl main <br/>. type main, @ function <br/> main: <br/> pushl % EBP <br/> movl % ESP, % EBP <br/> call test <br/> movl $0, % eax <br/> popl % EBP <br/> RET <br/>. size main ,. -Main <br/>. ident "GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)" <br/>. section. note. GNU-stack, "", @ progbits 

From the assembly code above, we can see that there is no overhead for Parameter Function calls:

1. Call the function and return, so you need to execute a call/RET command pair.

2. The function needs to protect the scene, so % EBP needs to be pushed to the stack, and pop it out before returning it.

3. Construct the function runtime environment and assign % EBP to the top % esp of the current stack.

The overhead of function call without parameters is five commands.

 

 

Program 2-function call with parameters:

# Include <stdio. h> <br/> void test (int A) <br/>{< br/> (a) ++; <br/> return; <br/>}< br/> int main (INT argc, char * argv []) <br/>{< br/> int A = 1; <br/> test (a); <br/> return 0; <br/>} 

Use gcc-s to get assembly code of Program 2:

. File "test. C "<br/>. text <br/>. globl test <br/>. type Test, @ function <br/> test: <br/> pushl % EBP <br/> movl % ESP, % EBP <br/> addl $1, 8 (% EBP) <br/> NOP <br/> popl % EBP <br/> RET <br/>. size Test ,. -Test <br/>. globl main <br/>. type main, @ function <br/> main: <br/> pushl % EBP <br/> movl % ESP, % EBP <br/> subl $20, % ESP <br/> movl $1,-4 (% EBP) <br/> movl-4 (% EBP), % eax <br/> movl % eax, (% ESP) <br/> call test <br/> movl $0, % eax <br/> Leave <br/> RET <br/>. size main ,. -Main <br/>. ident "GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)" <br/>. section. note. GNU-stack, "", @ progbits 

 

Compared with the overhead of Parameter Function calls, there are two more commands with Parameter Function calls for passing parameters:

Movl-4 (% EBP), % eax

Movl % eax, (% EBP)

Two commands are required for each parameter.

If it is a pointer parameter, two commands are required for the function to be used.

 

 

In this case, the overhead of function calling is quite large.

Therefore, when a function is small and frequently called, it should be replaced by a macro or inline function.

 

In addition, although function calls have overhead, unless necessary, the function should be used where the function is used. Otherwise, the readability and maintainability of the code will be seriously reduced.

 

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.