fork download
  1. #include <vector>
  2. #include <string>
  3. #include <list>
  4. #include <iostream>
  5. #include <ctime>
  6. #include <atlcoll.h>
  7. #include <memory>
  8.  
  9. static const int test_count = 1000000;
  10. class impl {
  11. public:
  12. template<class T> T* allocate(unsigned n, const T* hint, std::allocator<T>* al) {if (e-c>=n) {char* t(c); c+=n; return (T*)t;}return al->allocate(n, hint);}
  13. template<class T> void deallocate(T* p, unsigned n, std::allocator<T>* al) {if ((char*)p<(char*)this || (char*)p>e) al->deallocate(p, n);}
  14. static impl* construct(int size) {return new(new char[size+sizeof(impl)])impl(size); }
  15. void inc_ref_count() {++p;}
  16. void dec_ref_count() { if (--p == 0) {this->~impl(); delete[]((char*)this);}}
  17. private:
  18. char* c;
  19. char* e;
  20. int p;
  21. impl(int size): c(((char*)this)+sizeof(impl)), e(c+size), p(1){}
  22. impl(impl& nocopy);
  23. impl&operator=(impl& nocopy);
  24. };
  25.  
  26. template<class T>
  27. class pool_allocator : public std::allocator<T> {
  28. impl* al;
  29. public:
  30. typedef typename std::allocator<T>::pointer pointer;
  31. typedef typename std::allocator<T>::const_pointer const_pointer;
  32. template<class U> struct rebind {typedef pool_allocator<U> other;};
  33. template<class U> friend class pool_allocator;
  34.  
  35. pool_allocator() : std::allocator<T>(), al(impl::construct(4096)) {}
  36. explicit pool_allocator(unsigned size) : std::allocator<T>(), al(impl::construct(size)) {}
  37. pool_allocator(const pool_allocator& rhs) : std::allocator<T>(rhs), al(rhs.al) {al->inc_ref_count();}
  38. template<class U> pool_allocator(const pool_allocator<U>& rhs) : std::allocator<T>(rhs), al(rhs.al) {al->inc_ref_count();}
  39. ~pool_allocator() {al->dec_ref_count();}
  40. pointer allocate(size_type n, const_pointer hint = 0 ) {return al->allocate<T>(n*sizeof(T), hint, this);}
  41. void deallocate(pointer p, size_type n) {al->deallocate<T>(p, n*sizeof(T), this);}
  42. pool_allocator& operator=(const pool_allocator& rhs) {std::allocator<T>::operator=(rhs); al->dec_ref_count(); al=rhs.al; al->inc_ref_count();}
  43. template<class U> pool_allocator& operator=(const pool_allocator<U>& rhs) {std::allocator<T>::operator=(rhs); al->dec_ref_count(); al=rhs.al; al->inc_ref_count();}
  44. };
  45.  
  46. void list_stl()
  47. {
  48. pool_allocator<double> alloc(test_count*12);
  49. std::list<double/*, pool_allocator<double>*/> items/*(alloc)*/;
  50. std::cout << "10M doubles in std::list ";
  51. clock_t begin = clock();
  52. for( int i = 0; i < test_count; i++ )
  53. items.push_back( rand() );
  54. std::cout << (double(clock()-begin)/CLOCKS_PER_SEC) << '\n';
  55. }
  56.  
  57. void list_atl()
  58. {
  59. CAtlList<double> items;
  60. std::cout << "10M doubles in CAtlList " ;
  61. clock_t begin = clock();
  62. for( int i = 0; i < test_count; i++ )
  63. items.AddTail( rand() );
  64. std::cout << (double(clock()-begin)/CLOCKS_PER_SEC) << '\n';
  65. }
  66.  
  67. int main() {
  68. list_stl();
  69. list_stl();
  70. list_atl();
  71. list_atl();
  72. return 0;
  73. }
  74.  
  75. /*
  76. 10M doubles in std::list 0.453
  77. 10M doubles in std::list 0.453
  78. 10M doubles in CAtlList 0.188
  79. 10M doubles in CAtlList 0.187
  80.  
  81. 10M doubles in std::list 1.328
  82. 10M doubles in std::list 1.359
  83. 10M doubles in CAtlList 0.188
  84. 10M doubles in CAtlList 0.172
  85. */
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:6:21: error: atlcoll.h: No such file or directory
prog.cpp:40: error: ‘size_type’ has not been declared
prog.cpp:41: error: ‘size_type’ has not been declared
prog.cpp: In function ‘void list_stl()’:
prog.cpp:53: error: ‘rand’ was not declared in this scope
prog.cpp: In function ‘void list_atl()’:
prog.cpp:59: error: ‘CAtlList’ was not declared in this scope
prog.cpp:59: error: expected primary-expression before ‘double’
prog.cpp:59: error: expected `;' before ‘double’
prog.cpp:63: error: ‘items’ was not declared in this scope
prog.cpp:63: error: ‘rand’ was not declared in this scope
stdout
Standard output is empty