原生数组、std::array、std::vector访问性能对比
先上结论:
std::vector的at()操作最慢。其时间约达其他访问方式的2~4倍。原生数组和std::vector的[]访问较快。std::array的访问速度介于中间,约是原生的2倍。
具体测试方法:
主要测试读取和赋值访问,结果中:
t1 - std::array的at()时间
t2 - std::array的[]时间
t3 - 原生数组时间
t4 - std::vector的at()时间
t2 - std::vector的[]时间
访问:
auto now() { struct timeval tvt; gettimeofday(&tvt, 0); return tvt.tv_sec * 1000000 + tvt.tv_usec; } void test() { std::array<int, 10> a = {1, 2 , 3, 4, 5, 6, 7, 8, 9, 10}; int times = 1000000; auto t1 = now(); for (int i = 0; i < times; ++i) { a.at(1); } std::cout << "t1:" << now() - t1 << std::endl; auto t2 = now(); for (int i = 0; i < times; ++i) { a[1]; } std::cout << "t2:" << now() - t2 << std::endl; int b[10] = {0}; auto t3 = now(); for (int i = 0; i < times; ++i) { b[1]; } std::cout << "t3:" << now() - t3 << std::endl; std::vector<int> v = {1, 2 , 3, 4, 5, 6, 7, 8, 9, 10}; auto t4 = now(); for (int i = 0; i < times; ++i) { v.at(1); } std::cout << "t4:" << now() - t4 << std::endl; auto t5 = now(); for (int i = 0; i < times; ++i) { v[1]; } std::cout << "t5:" << now() - t5 << std::endl; } int main() { for (int i = 0; i < 10; ++i) { std::cout << "n:" << i << std::endl; test(); } }
数据结果:
n:0 t1:3823 t2:3925 t3:2444 t4:8292 t5:1757 n:1 t1:3939 t2:3914 t3:1997 t4:8247 t5:1764 n:2 t1:3846 t2:3915 t3:2169 t4:8260 t5:1764 n:3 t1:3761 t2:3895 t3:2161 t4:8376 t5:1869 n:4 t1:3868 t2:3916 t3:2291 t4:8303 t5:1763 n:5 t1:3788 t2:3928 t3:2448 t4:8241 t5:1761 n:6 t1:3760 t2:3914 t3:2193 t4:8336 t5:1762 n:7 t1:3876 t2:3908 t3:2159 t4:8255 t5:1805 n:8 t1:3869 t2:4111 t3:2488 t4:8283 t5:1865 n:9 t1:3737 t2:3909 t3:2271 t4:8292 t5:1761
赋值:
#include <iostream> #include <vector> #include <array> #include <sys/time.h> auto now() { struct timeval tvt; gettimeofday(&tvt, 0); return tvt.tv_sec * 1000000 + tvt.tv_usec; } void test() { std::array<int, 10> a = {1, 2 , 3, 4, 5, 6, 7, 8, 9, 10}; int times = 1000000; auto t1 = now(); for (int i = 0; i < times; ++i) { a.at(1) = 1; } std::cout << "t1:" << now() - t1 << std::endl; auto t2 = now(); for (int i = 0; i < times; ++i) { a[1] = 1; } std::cout << "t2:" << now() - t2 << std::endl; int b[10] = {0}; auto t3 = now(); for (int i = 0; i < times; ++i) { b[1] = 1; } std::cout << "t3:" << now() - t3 << std::endl; std::vector<int> v = {1, 2 , 3, 4, 5, 6, 7, 8, 9, 10}; auto t4 = now(); for (int i = 0; i < times; ++i) { v.at(1) = 1; } std::cout << "t4:" << now() - t4 << std::endl; auto t5 = now(); for (int i = 0; i < times; ++i) { v[1] = 1; } std::cout << "t5:" << now() - t5 << std::endl; } int main() { for (int i = 0; i < 10; ++i) { std::cout << "n:" << i << std::endl; test(); } }
数据结果:
n:0 t1:4032 t2:4057 t3:2204 t4:8628 t5:2005 n:1 t1:3950 t2:4119 t3:2292 t4:8311 t5:2004 n:2 t1:3948 t2:4061 t3:2366 t4:8305 t5:2005 n:3 t1:3956 t2:4057 t3:2144 t4:8496 t5:2007 n:4 t1:3853 t2:4058 t3:2213 t4:8343 t5:2116 n:5 t1:4001 t2:4056 t3:2091 t4:8460 t5:2008 n:6 t1:3968 t2:4175 t3:2092 t4:8310 t5:2005 n:7 t1:3963 t2:4130 t3:1819 t4:8408 t5:2038 n:8 t1:4138 t2:4190 t3:2188 t4:8452 t5:2054 n:9 t1:3964 t2:4058 t3:2328 t4:8375 t5:2007