65,170
社区成员




int a[10] = {12,3,45,6,7,2,45,5,2,10};
vector<int> S(a,a+10);
S.swap(vector<int>());
S.swap(vector<int>(3));
void swap(vector<_Tp, _Alloc>& __x) {
__STD::swap(_M_start, __x._M_start);//一次
__STD::swap(_M_finish, __x._M_finish);//二次
__STD::swap(_M_end_of_storage, __x._M_end_of_storage);//三次
}
vector<int> S;
int* p = S.get_allocator().allocate(10);
S.get_allocator().deallocate(p,10);
vector<double> x;
//x......
vector<double>(x).swap(x);
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <time.h>
#include <boost/pool/pool_alloc.hpp>
template<typename T>
class alloc
{
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
public:
alloc(){}
template<typename T>
alloc(const alloc<T>& ){
}
alloc& operator=(const alloc& rhs){
return *this;
}
static pointer allocate(size_type n)
{
return (pointer)malloc(n*sizeof(T));
}
static void deallocate(pointer ptr, size_type n)
{
free((pointer)ptr);
}
size_type max_size() const throw()
{
return size_t(-1) / sizeof(T);
}
template <typename U>
struct rebind
{
typedef alloc<U> other;
};
};
int _tmain(int argc, _TCHAR* argv[])
{
int start4 = GetTickCount();
std::vector<int, alloc<int>> RR4;
for (int i = 0; i < 10000000; i++)
{
RR4.push_back(i);
}
int time4 = GetTickCount()- start4;
std::cout<<"alloc: "<<time4<<std::endl;
int start3 = GetTickCount();
std::vector<int, boost::fast_pool_allocator<int>> RR3;
for (int i = 0; i < 10000000; i++)
{
RR3.push_back(i);
}
int time3 = GetTickCount()- start3;
std::cout<<"boost::fast_pool_allocator: "<<time3<<std::endl;
int start1 = GetTickCount();
std::vector<int,std::allocator<int>> RR1;
for (int i = 0; i < 10000000; i++)
{
RR1.push_back(i);
}
int time1 = GetTickCount()- start1;
std::cout<<"std::allocator: "<<time1<<std::endl;
int start2 = GetTickCount();
std::vector<int, boost::pool_allocator<int>> RR2;
for (int i = 0; i < 10000000; i++)
{
RR2.push_back(i);
}
int time2 = GetTickCount()- start2;
std::cout<<"boost::pool_allocator: "<<time2<<std::endl;
return 0;
}
alloc: 11438
boost::fast_pool_allocator: 19437
std::allocator: 11562
boost::pool_allocator: 27703
请按任意键继续. . .
alloc: 375
boost::fast_pool_allocator: 1032
std::allocator: 343
boost::pool_allocator: 1266
请按任意键继续. . .