原生数组、std::array、std::vector访问性能对比

发布时间 2023-03-22 21:13:52作者: 倾越

先上结论:

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();
    }
}

数据结果: