Speed:
- Calling a function means pushing to the stack, then jumping, then storing on the stack again, then jumping again. if you use parameters to the function, you usually have several more pushes.
Consider a loop:
for...
func1
inside a loop, all those pushes, and jumps can be a factor.
This was largely solved with the presentation of Inline Functions on C99 and unofficially before that, But some code written before, or was created with compatibility in mind, may have been long for that reason.
Also Inline has it's flows, some are described on the Inline Functions link.
Edit:
As an example of how a call to a function can make a program slower:
4 static void
5 do_printf()
6 {
7 printf("hi");
8 }
9 int
10 main()
11 {
12 int i=0;
13 for(i=0;i<1000;++i)
14 do_printf();
15 }
This produces (GCC 4.2.4):
.
.
jmp .L4
.L5:
call do_printf
addl $1, -8(%ebp)
.L4:
cmpl $999, -8(%ebp)
jle .L5
.
.
do_printf:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
movl $.LC0, (%esp)
call printf
leave
ret
against:
int
main()
{
int i=0;
for(i=0;i<1000;++i)
printf("hi");
}
or against:
4 static inline void __attribute__((always_inline)) //This is GCC specific!
5 do_printf()
6 {
7 printf("hi");
8 }
Both produce (GCC 4.2.4):
jmp .L2
.L3:
movl $.LC0, (%esp)
call printf
addl $1, -8(%ebp)
.L2:
cmpl $999, -8(%ebp)
jle .L3
Which is faster.