i was solving algorithm problems and wondering why my code is about twice slower than others even the logic was same.
Then i figured out using lambda or free function is much faster than member function.
i googled about it but most of the people says there are no difference.
i checked excution time on my computer, the results are below:
its a simple fibonacci function within/without class. Member function is about 0.3sec slower than free function.
class bma{
public:
int dfs(int a){
if(a <= 1) return 1;
return dfs(a-1)+dfs(a-2);
}
};
int dfs(int a){
if(a <= 1) return 1;
return dfs(a-1)+dfs(a-2);
}
int32_t main(){
bma bm;
time_t start,end;
double result;
for(int T=1; T<= 5; T++){
start = clock();
bm.dfs(42);
end = clock();
result = (double)(end-start);
cout << T << "th class func exec time:" << result/CLOCKS_PER_SEC << endl;
}
for(int T=1; T<= 5; T++){
start = clock();
dfs(42);
end = clock();
result = (double)(end-start);
cout << T << "th norm func exec time:" << result/CLOCKS_PER_SEC << endl;
}
}
is there something i don't know about C++ or process of calling a function?
