Created
December 20, 2013 03:51
-
-
Save benloong/8050171 to your computer and use it in GitHub Desktop.
c++11 direct function call, virtual function call, functor and std::function(using a lambda) performance test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <chrono> | |
#include <memory> | |
#include <functional> | |
using namespace std; | |
using namespace std::chrono; | |
class Base | |
{ | |
public: | |
Base(){} | |
virtual ~Base(){} | |
virtual int func(int i) = 0; | |
}; | |
class Derived : public Base | |
{ | |
public: | |
Derived(int base = 10) : base{base} | |
{ | |
} | |
~Derived(){} | |
virtual int func(int i) | |
{ | |
return i*base; | |
} | |
private: | |
int base; | |
}; | |
struct Func | |
{ | |
int base; | |
int operator()(int i) | |
{ | |
return i*base; | |
} | |
Func(int base) : base {base} | |
{ | |
} | |
}; | |
const int base = 10; | |
int calculate(int i) | |
{ | |
return base*i; | |
} | |
int main() | |
{ | |
const int num = 10000; | |
Base *p = new Derived{10}; | |
int total = 0; | |
auto start = high_resolution_clock::now(); | |
for (int i = 0; i < num; ++i) | |
{ | |
total += p->func(i); | |
} | |
auto end = high_resolution_clock::now(); | |
std::cout<<"result: "<<total<<"\nvirtual call elapsed: \t"<<duration_cast<nanoseconds>(end-start).count()<<" nanoseconds.\n"<<std::endl; | |
total = 0; | |
start = high_resolution_clock::now(); | |
for (int i = 0; i < num; ++i) | |
{ | |
total += calculate(i); | |
} | |
end = high_resolution_clock::now(); | |
std::cout<<"result: "<<total<<"\ndirect function call elapsed: \t"<<duration_cast<nanoseconds>(end-start).count()<<" nanoseconds.\n"<<std::endl; | |
Func functor{10}; | |
total = 0; | |
start = high_resolution_clock::now(); | |
for (int i = 0; i < num; ++i) | |
{ | |
total += functor(i); | |
} | |
end = high_resolution_clock::now(); | |
std::cout<<"result: "<<total<<"\nfunctor call elapsed: \t"<<duration_cast<nanoseconds>(end-start).count()<<" nanoseconds.\n"<<std::endl; | |
int base = 10; | |
function<int(int)> lambda = [base](int i) | |
{ | |
return i*base; | |
}; | |
total = 0; | |
start = high_resolution_clock::now(); | |
for (int i = 0; i < num; ++i) | |
{ | |
total += lambda(i); | |
} | |
end = high_resolution_clock::now(); | |
std::cout<<"result: "<<total<<"\nlambda call elapsed: \t"<<duration_cast<nanoseconds>(end-start).count()<<" nanoseconds.\n"<<std::endl; | |
return 0; | |
} | |
/* | |
test on mac mini i7 2.7GHz | |
clang++ -std=c++11 chronotest.cpp -O0 | |
output: | |
result: 499950000 | |
virtual call elapsed: 43171 nanoseconds. | |
result: 499950000 | |
direct function call elapsed: 31379 nanoseconds. | |
result: 499950000 | |
functor call elapsed: 41497 nanoseconds. | |
result: 499950000 | |
lambda call elapsed: 207416 nanoseconds. | |
=================================================== | |
clang++ -std=c++11 chronotest.cpp -O1 | |
output: | |
result: 499950000 | |
virtual call elapsed: 26144 nanoseconds. | |
result: 499950000 | |
direct function call elapsed: 22384 nanoseconds. | |
result: 499950000 | |
functor call elapsed: 33477 nanoseconds. | |
result: 499950000 | |
lambda call elapsed: 55799 nanoseconds. | |
=================================================== | |
clang++ -std=c++11 chronotest.cpp -O2 | |
result: 499950000 | |
virtual call elapsed: 22284 nanoseconds. | |
result: 499950000 | |
direct function call elapsed: 36 nanoseconds. | |
result: 499950000 | |
functor call elapsed: 30 nanoseconds. | |
result: 499950000 | |
lambda call elapsed: 28292 nanoseconds. | |
=================================================== | |
clang++ -std=c++11 chronotest.cpp -O3 | |
result: 499950000 | |
virtual call elapsed: 18975 nanoseconds. | |
result: 499950000 | |
direct function call elapsed: 29 nanoseconds. | |
result: 499950000 | |
functor call elapsed: 30 nanoseconds. | |
result: 499950000 | |
lambda call elapsed: 22542 nanoseconds. | |
=================================================== | |
clang++ -std=c++11 chronotest.cpp -O4 | |
result: 499950000 | |
virtual call elapsed: 22141 nanoseconds. | |
result: 499950000 | |
direct function call elapsed: 30 nanoseconds. | |
result: 499950000 | |
functor call elapsed: 30 nanoseconds. | |
result: 499950000 | |
lambda call elapsed: 22584 nanoseconds. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment