晚上在虚拟机上安装了 google benchmark 运行了一个网上的例子,运行 ok,自己写了一个始终显示都是 0ns 耗时,请求各位大佬分析,哪里有问题?
#include <benchmark/benchmark.h>
#include <vector>
int loop = 100000;
static void bench_vector_reserve(benchmark::State& state)
{
std::vector<int> ans;
ans.reserve(loop);
for(int i = 0;i < loop;i++)
ans.push_back(i);
}
BENCHMARK(bench_vector_reserve);
static void bench_vector(benchmark::State& state)
{
std::vector<int> ans;
for(int i = 0;i < loop;i++)
ans.push_back(i);
}
BENCHMARK(bench_vector);
BENCHMARK_MAIN();
最近学习 C++了解到 vector 会动态扩容,比如每次空间 double,我上面的例子不管 loop 调多大,运行结果都显示:
[root workspace]#./a.out
2021-08-13T12:00:19-04:00
Running ./a.out
Run on (4 X 1900 MHz CPU s)
CPU Caches:
L1 Data 32 KiB (x2)
L1 Instruction 32 KiB (x2)
L2 Unified 256 KiB (x2)
L3 Unified 3072 KiB (x1)
Load Average: 0.02, 0.03, 0.00
---------------------------------------------------------------
Benchmark Time CPU Iterations
---------------------------------------------------------------
bench_vector_reserve 0.000 ns 0.000 ns 0
bench_vector 0.000 ns 0.000 ns 0
[root workspace]#
有没有帮忙老哥分析一波?