#include "stdafx.h"<br />#include <iostream><br />#include <vector><br />#include <time.h><br />#include <boost/pool/pool_alloc.hpp><br />template<typename T><br />class alloc<br />{<br />public:<br />typedef std::size_t size_type;<br />typedef std::ptrdiff_t difference_type;<br />typedef T* pointer;<br />typedef const T* const_pointer;<br />typedef T& reference;<br />typedef const T& const_reference;<br />typedef T value_type;<br />public:<br /> alloc(){}<br /> template<typename T><br /> alloc(const alloc<T>& ){<br /> }<br /> alloc& operator=(const alloc& rhs){<br /> return *this;<br /> }<br /> static pointer allocate(size_type n)<br /> {<br /> return (pointer)malloc(n*sizeof(T));<br /> }<br />static void deallocate(pointer ptr, size_type n)<br />{<br />free((pointer)ptr);<br />}<br />size_type max_size() const throw()<br />{<br />return size_t(-1) / sizeof(T);<br />}<br />template <typename U><br />struct rebind<br />{<br />typedef alloc<U> other;<br />};</p><p>};<br />int _tmain(int argc, _TCHAR* argv[])<br />{ </p><p>int start4 = GetTickCount();<br />std::vector<int, alloc<int>> RR4;<br />for (int i = 0; i < 10000000; i++)<br />{<br />RR4.push_back(i);</p><p>}<br />int time4 = GetTickCount()- start4;<br />std::cout<<"alloc: "<<time4<<std::endl;<br />int start3 = GetTickCount();<br />std::vector<int, boost::fast_pool_allocator<int>> RR3;<br />for (int i = 0; i < 10000000; i++)<br />{<br />RR3.push_back(i);<br />}<br />int time3 = GetTickCount()- start3;<br />std::cout<<"boost::fast_pool_allocator: "<<time3<<std::endl;<br />int start1 = GetTickCount();<br />std::vector<int,std::allocator<int>> RR1;<br />for (int i = 0; i < 10000000; i++)<br />{<br />RR1.push_back(i);<br />}<br />int time1 = GetTickCount()- start1;<br />std::cout<<"std::allocator: "<<time1<<std::endl;</p><p>int start2 = GetTickCount();<br />std::vector<int, boost::pool_allocator<int>> RR2;<br />for (int i = 0; i < 10000000; i++)<br />{<br />RR2.push_back(i);<br />}<br />int time2 = GetTickCount()- start2;<br />std::cout<<"boost::pool_allocator: "<<time2<<std::endl;<br />return 0;<br />}
debug (VC2008):
alloc: 11438<br />boost::fast_pool_allocator: 19437<br />std::allocator: 11562<br />boost::pool_allocator: 27703<br />請按任意鍵繼續. . .
Release(VC2008):
alloc: 375<br />boost::fast_pool_allocator: 1032<br />std::allocator: 343<br />boost::pool_allocator: 1266<br />請按任意鍵繼續. . .