The code runs ten times slower when all I have done is translate the loop code into a function

I ran the code below, which is substantially small. It just adds 2 and four 100 million times and prints the runtime.

#include "time.h"
#include <iostream>
using namespace std;

void add (){
int tot = 2+4;
}

void main(){
int t = clock();
int count = 0;
while(count<100000000){
    int tot = 2+4;
    count++;
}
cout <<endl<< "runtime = " << fixed <<(double) (clock() - t) / CLOCKS_PER_SEC <<"s" << endl;

}

But I was interested to see the time difference when I did the same thing, but called the function. So I replaced the line "int tot = 2 + 4" with "add ()".

I expected the second runtime to be a little longer, but it was much longer. First implementation = .3sand second implementation = 3s.

I understand that calling a function requires using a stack to store the return address and store local data. But should it be much more than that?

, - , , , , - .

+5
3

, , , .

( ) 2 + 4 6

mov eax, 6 ;eax is just an example here or
mov tot_addr, 6 ; mem->mem mov

, ,

push 4 ;assuming 4 byte ints
push 4 ;wrote 2 to be clear that you have to push both variables
call add

- . ( push ). , , RET . , ,

mov eax, 4
add eax, 2

, , , ( )

: . , , , , , CALL . ,

mov eax, 4
mov ecx, 2
push 4 ; size for return value
push eax
push ecx
call add

mov eax, 4
mov exc, 2
add eax, ecx

:

int a = 4;
int b = 2;
int res = add(a, b);

int a = 4;
int b = 2;
int res = a + b;

, .

+5

(, ), , . inline / , .

+2

CPU, .

. i-cache . , 10 , .

.

0

All Articles