0

I use google benchmark test the performance of array and vector

link:

https://quick-bench.com/q/ixWRn2XG8Q1-OnSFP6GXsadsw_g and you can see the code with the link

result:

enter image description here it shows that use array is almost no cost, array is 2900000000 times faster than vector ### question so why this happens?

my code

#include<vector>
#include <vector>
static void int_use_array(benchmark::State& state) {
    for (auto _ : state) {
        int a[200][200];
        for(int i = 0; i < 200; ++i) {
            for(int j = 0; j < 200; ++j) {
                a[i][j] = i * j;
            }
        }
        int t = a[2][89];
    }
}
BENCHMARK(int_use_array);
static void int_use_vector(benchmark::State& state) {
    for (auto _ : state) {
        std::vector<std::vector<int> > vec(200, std::vector<int>(200, 0));
        for (int i = 0; i < 200; ++i) {
            for(int j = 0; j < 200; j++) {
                vec[i][j] = i * j;
            }
         }
        int t = vec[2][98];
    }
}
BENCHMARK(int_use_vector);

0 Answers0