When I run the following code on my system, the inline function virtually always runs slower than the non-inline function. Why is this?
Code:#include <iostream> #include <ctime> using namespace std; inline void f1() { int n = 0; for (int i = 0; i < 100; i++) n += 10; } void f2() { int n = 0; for (int i = 0; i < 100; i++) n += 10; } int main() { clock_t start; clock_t end; clock_t duration; cout << "Running inline function f1()..." << endl; start = clock(); for (int i = 0; i < 1500000; i++) f1(); end = clock(); duration = end - start; cout << "Time elapsed: " << duration << " ticks." << endl; cout << "Running function f2()..." << endl; start = clock(); for (int i = 0; i < 1500000; i++) f2(); end = clock(); duration = end - start; cout << "Time elapsed: " << duration << " ticks." << endl; system("pause"); return 0; }



LinkBack URL
About LinkBacks



