#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <boost/pool/pool_alloc.hpp>
__attribute__ ((noinline))
int nofunc(int count) {
return count;
}
__attribute__ ((noinline))
int allocatorfunc(int count) {
char* a = new char[count];
a[count-1] = (char)count;
int nocheat = a[count-1];
delete[] a;
return nocheat;
}
__attribute__ ((noinline))
int allocafunc(int count) {
char* a = (char*)alloca(count);
a[count-1] = (char)count;
int nocheat = a[count-1];
return nocheat;
}
__attribute__ ((noinline))
int poolfunc(int count) {
boost::pool_allocator<char> p;
char* a = p.allocate(count);
a[count-1] = (char)count;
int nocheat = a[count-1];
p.deallocate(a);
return nocheat;
}
int main() {
#ifdef _DEBUG
int count=100000;
#else
int count=10000000;
#endif
int nocheat = 0;
clock_t begin = 0;
typedef int (*funcptr)(int);
funcptr functions[3] = {nofunc, allocatorfunc, allocafunc};
char* names[3] = {"nofunc", "allocator", "alloca"};
try {
for(int j=0; j<3; ++j) {
srand(0);
nocheat = 0;
begin = clock();
for(int i=0; i<count; ++i)
nocheat += functions[j](1+rand()%4000);
double overhead = double(clock()-begin)/CLOCKS_PER_SEC;
std::cout << names[j] << " took " << overhead << " (" << nocheat << ")\n";
}
} catch(std::exception& e) {
std::cout << e.what();
throw;
}
return 0;
}