fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <boost/pool/pool_alloc.hpp>
  6.  
  7. __attribute__ ((noinline))
  8. int nofunc(int count) {
  9. return count;
  10. }
  11.  
  12. __attribute__ ((noinline))
  13. int allocatorfunc(int count) {
  14. char* a = new char[count];
  15. a[count-1] = (char)count;
  16. int nocheat = a[count-1];
  17. delete[] a;
  18. return nocheat;
  19. }
  20.  
  21. __attribute__ ((noinline))
  22. int allocafunc(int count) {
  23. char* a = (char*)alloca(count);
  24. a[count-1] = (char)count;
  25. int nocheat = a[count-1];
  26. return nocheat;
  27. }
  28.  
  29. boost::pool_allocator<char> pa;
  30. __attribute__ ((noinline))
  31. int poolfunc(int count) {
  32. char* a = pa.allocate(count);
  33. a[count-1] = (char)count;
  34. int nocheat = a[count-1];
  35. pa.deallocate(a, count);
  36. return nocheat;
  37. }
  38.  
  39. boost::fast_pool_allocator<char> fpa;
  40. __attribute__ ((noinline))
  41. int fastpoolfunc(int count) {
  42. char* a = fpa.allocate(count);
  43. a[count-1] = (char)count;
  44. int nocheat = a[count-1];
  45. fpa.deallocate(a, count);
  46. return nocheat;
  47. }
  48.  
  49. int main() {
  50. #ifdef _DEBUG
  51. int count=100000;
  52. #else
  53. int count=1000000;
  54. #endif
  55. int nocheat = 0;
  56. clock_t begin = 0;
  57.  
  58. typedef int (*funcptr)(int);
  59. funcptr functions[5] = {nofunc, allocatorfunc, allocafunc, poolfunc, fastpoolfunc};
  60. const char* names[5] = {"nofunc", "allocator", "alloca", "boostpool", "fastpool"};
  61.  
  62. try {
  63. for(int j=0; j<5; ++j) {
  64. srand(0);
  65. nocheat = 0;
  66. begin = clock();
  67. for(int i=0; i<count; ++i)
  68. nocheat += functions[j](1+rand()%4000);
  69. double overhead = double(clock()-begin)/CLOCKS_PER_SEC;
  70. std::cout << names[j] << " took " << overhead << " (" << nocheat << ")\n";
  71. }
  72. } catch(std::exception& e) {
  73. std::cout << e.what();
  74. throw;
  75. }
  76.  
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 3.69s 2864KB
stdin
Standard input is empty
stdout
nofunc took 0.02 (2002597283)
allocator took 0.17 (532643)
alloca took 0.04 (532643)
boostpool took 1.72 (532643)
fastpool took 1.71 (532643)