#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;
}
boost::pool_allocator<char> pa;
__attribute__ ((noinline))
int poolfunc(int count) {
char* a = pa.allocate(count);
a[count-1] = (char)count;
int nocheat = a[count-1];
pa.deallocate(a, count);
return nocheat;
}
boost::fast_pool_allocator<char> fpa;
__attribute__ ((noinline))
int fastpoolfunc(int count) {
char* a = fpa.allocate(count);
a[count-1] = (char)count;
int nocheat = a[count-1];
fpa.deallocate(a, count);
return nocheat;
}
int main() {
#ifdef _DEBUG
int count=100000;
#else
int count=1000000;
#endif
int nocheat = 0;
clock_t begin = 0;
typedef int (*funcptr)(int);
funcptr functions[5] = {nofunc, allocatorfunc, allocafunc, poolfunc, fastpoolfunc};
const char* names[5] = {"nofunc", "allocator", "alloca", "boostpool", "fastpool"};
try {
for(int j=0; j<5; ++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;
}