12-boost-array

#include<boost/array.hpp>


测试普通数组、boost::array ,std::vector 的效率

效率 普通数组 > boost::array > std::vector

boost::array 更灵活


const int N = 1000;

template<typename T>

void Work(T arr)

{

for(size_t i = 0;i<N; i++){

for(size_t j=0;j<N; j++){

arr[i] = arr[j];

}

}

}


#include<vector>

#include<boost/timer/timer.hpp>

普通数组

{

int arr[N] = {0}

auto_cpu_timer t(6,"普通数组消耗时间 %w \n");

Work<int[]>(arr);

}


boost::array

{

boost::array<int N>arr= {0}

auto_cpu_timer t(6,"boost::array数组消耗时间 %w \n");

Work<boost::array<int N>>(arr);

}


std::vector

{

vector<int> arr;

arr.resize(N);

auto_cpu_timer t(6,"std::vector数组消耗时间 %w \n");

Work<vector<int>>(arr);

}