2 // shared_ptr_alloc_test.cpp - use to evaluate the impact of count allocations
4 // Copyright (c) 2002, 2003 Peter Dimov
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
11 #include <boost/shared_ptr.hpp>
12 #include <boost/config.hpp>
13 #include <boost/detail/quick_allocator.hpp>
21 int const n = 1024 * 1024;
23 template<class T> void test(T * = 0)
25 std::clock_t t = std::clock();
29 std::vector< boost::shared_ptr<T> > v;
31 for(int i = 0; i < n; ++i)
33 boost::shared_ptr<T> pi(new T(i));
40 std::clock_t t3 = std::clock();
42 std::cout << " " << static_cast<double>(t3 - t) / CLOCKS_PER_SEC << " seconds, " << static_cast<double>(t2 - t) / CLOCKS_PER_SEC << " + " << static_cast<double>(t3 - t2) / CLOCKS_PER_SEC << ".\n";
49 explicit X(int n): n_(n)
53 void * operator new(std::size_t)
55 return std::allocator<X>().allocate(1, static_cast<X*>(0));
58 void operator delete(void * p)
60 std::allocator<X>().deallocate(static_cast<X*>(p), 1);
66 X & operator=(X const &);
75 explicit Y(int n): n_(n)
79 void * operator new(std::size_t n)
81 return boost::detail::quick_allocator<Y>::alloc(n);
84 void operator delete(void * p, std::size_t n)
86 boost::detail::quick_allocator<Y>::dealloc(p, n);
92 Y & operator=(Y const &);
101 explicit Z(int n): Y(n), m_(n + 1)
108 Z & operator=(Z const &);
115 std::cout << BOOST_COMPILER "\n";
116 std::cout << BOOST_PLATFORM "\n";
117 std::cout << BOOST_STDLIB "\n";
119 #if defined(BOOST_HAS_THREADS)
120 std::cout << "BOOST_HAS_THREADS: (defined)\n";
122 std::cout << "BOOST_HAS_THREADS: (not defined)\n";
125 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
126 std::cout << "BOOST_SP_USE_STD_ALLOCATOR: (defined)\n";
128 std::cout << "BOOST_SP_USE_STD_ALLOCATOR: (not defined)\n";
131 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
132 std::cout << "BOOST_SP_USE_QUICK_ALLOCATOR: (defined)\n";
134 std::cout << "BOOST_SP_USE_QUICK_ALLOCATOR: (not defined)\n";
137 #if defined(BOOST_QA_PAGE_SIZE)
138 std::cout << "BOOST_QA_PAGE_SIZE: " << BOOST_QA_PAGE_SIZE << "\n";
140 std::cout << "BOOST_QA_PAGE_SIZE: (not defined)\n";
143 std::cout << n << " shared_ptr<int> allocations + deallocations:\n";
149 std::cout << n << " shared_ptr<X> allocations + deallocations:\n";
155 std::cout << n << " shared_ptr<Y> allocations + deallocations:\n";
161 std::cout << n << " shared_ptr<Z> allocations + deallocations:\n";