Testing the performance factors of C-language function call test two

Source: Internet
Author: User

function call : called function call called function, call function stack, called function execution, call function out of the stack, call function continue to execute a seemingly simple process, the system bottom has done a lot of operations.

Operation:

1, call function frame pointer (function parameter, local variable, stack frame state value, function return address) into the stack, stack pointer self-subtraction

2, save the state data of the calling function into the register

3, the function frame pointer is called into the stack, the current called function is executed

4, the called function execution ends, the stack is returned to the calling function of the frame pointer, from the register to restore the current state data

5, continue to execute the calling function until the end

That is, the entire invocation operation has a compression stack out of the stack, saving and recovering the state data process. The system stack memory has a default intrinsic size. How many times the function call will allocate how many stack frames. Therefore, the function call performance impact has the following factors:

1, function recursive layer number;

2, number of parameters (the size of the memory that the parameter signature occupies)

2.1 The number of different parameters of the same type;

2.2 Parameter types with different number of parameters;

2.3 The same parameter number, but the parameter type is different from the memory size;

3, the function stack size, that is, the function of the local variables of the stack size.

To test the performance (time consuming) factor of the C function call, a simple program is written to run in the following environment:

Ubuntu systems in VMware virtual machines on Windows7 operating systems

At the beginning and end of the function call, use the clock () function in time.h to return the number of CPU clock ticks (StartTime and endtime in the table below), with durationtime=endtime-starttime representing the time consumption of the function call. As follows:

clock_t Starttime=clock ();

Function call ...

clock_t Endtime=clock ();

Divide by Clocks_per_sec to get the time result in seconds

Double Durationtime= (double) (endtime-starttime)/clocks_per_sec;// represents the time the function call consumes the CPU, excluding the time of the child process or the operation of printf, etc.

Note: Detailed explanation of the clock () function URL: http://site.douban.com/199048/widget/notes/12005386/note/253542964/

A function recursive layer number ( loop 1 million times )

Number of recursive layers

Number of parameters

function stack size (bytes

Durationtime (seconds) [average]

Average time consumed per function call (nanoseconds)

10

5

1024

0.468

46.8

20

5

1024

0.87

43.5

30

5

1024

1.26

42

50

5

1024

2.39

47.8

100

5

1024

4.778

47.78

500

5

1024

26.394

52.788

1000

5

1024

57.726

57.726

1500

5

1024

93. 25

62.167

2000

5

1024

125.75

62.287

3000

5

1024

185.46

61.45

5000

5

1024

322.25

64.45

6000

5

1024

524.49

87.38

7000

5

1024

904.35

129.2

Note: average per function call time consumption =durationtime/call layer/number of cycles

function calls consume time line graphs:

Each time the function call consumes the line graph on average:

Conclusion:1, the function time consumption and the function call layer are almost equal to the growth of equivalent multiples;

2, the average time spent on each function call grows as the number of call tiers grows

Two Number of parameters ( loop 1000000 times)

Number of recursive layers

Number of parameters

function stack size (bytes)

Durationtime (seconds) [average]

Average time consumed per function call (nanoseconds)

1000

1

1024

55.19

55.19

1000

3

1024

54.093

54.093

1000

5

1024

57.603

57.603

1000

10

1024

57.4567

57.4567

Note: average per function call time consumption =durationtime/call layer/number of cycles

function calls consume time line graphs:

Average time consuming line graph per function call: For example, just the time consumption unit is changed to nanoseconds

Three function Stack Size

Number of recursive layers

Number of parameters

Large function stack (bytes)

Durationtime (seconds) [average]

Average time consumed per function call (nanoseconds)

1000

5

16

5.8267

5.8267

1000

5

32

7.035

7.035

1000

5

64

13.453

13.453

1000

5

128

16.163

16.163

1000

5

256

24.063

24.063

1000

5

512

40.355

40.355

1000

5

1024

57.603

57.603

1000

5

2048

88.553

88.553

1000

5

3072

1000

5

4096

Testing the performance factors of C-language function call test two

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.