vector<int>::push_back 与直接访问性能对比
vector 是提前开好空间然后每次 clear,vector2 是 reserve,list 就是 list,array 是对 vector vec[cur++] = i;
。这里都是整数,push_back 与 emplace_back 整体没有什么区别,但有人要看,所以函数名前面加了 e 的是 emplace_back。
测试结果:array < vector = vector2 << list
2024-11-15T22:10:51+08:00
Running ./push_back
Run on (8 X 2419.2 MHz CPU s)
CPU Caches:
L1 Data 48 KiB (x4)
L1 Instruction 32 KiB (x4)
L2 Unified 1280 KiB (x4)
L3 Unified 8192 KiB (x1)
Load Average: 0.08, 0.02, 0.01
-----------------------------------------------------------------
Benchmark Time CPU Iterations
-----------------------------------------------------------------
bench_evector/100 59.5 ns 59.5 ns 12706596
bench_evector/1000 538 ns 538 ns 1260305
bench_evector/10000 5438 ns 5438 ns 131222
bench_evector/100000 82071 ns 82071 ns 8507
bench_evector/1000000 868177 ns 868180 ns 817
bench_evector2/100 73.6 ns 73.6 ns 9367293
bench_evector2/1000 536 ns 536 ns 1290501
bench_evector2/10000 5057 ns 5057 ns 137541
bench_evector2/100000 81939 ns 81939 ns 8573
bench_evector2/1000000 860870 ns 860872 ns 816
bench_elist/100 1240 ns 1240 ns 558806
bench_elist/1000 12513 ns 12513 ns 53484
bench_elist/10000 126142 ns 126143 ns 5498
bench_elist/100000 1302104 ns 1302107 ns 537
bench_elist/1000000 14266316 ns 14266308 ns 48
bench_vector/100 55.7 ns 55.7 ns 12734427
bench_vector/1000 536 ns 536 ns 1282267
bench_vector/10000 5360 ns 5360 ns 129287
bench_vector/100000 81479 ns 81479 ns 8613
bench_vector/1000000 878694 ns 878296 ns 806
bench_vector2/100 74.0 ns 74.0 ns 9416006
bench_vector2/1000 542 ns 542 ns 1286119
bench_vector2/10000 5053 ns 5053 ns 134027
bench_vector2/100000 81568 ns 81567 ns 8460
bench_vector2/1000000 852110 ns 852105 ns 803
bench_list/100 1291 ns 1291 ns 552344
bench_list/1000 12544 ns 12544 ns 56783
bench_list/10000 128511 ns 128511 ns 5560
bench_list/100000 1300553 ns 1300559 ns 549
bench_list/1000000 14576666 ns 14576566 ns 47
bench_array/100 35.2 ns 35.2 ns 19917275
bench_array/1000 300 ns 300 ns 2356720
bench_array/10000 2961 ns 2961 ns 237905
bench_array/100000 29223 ns 29198 ns 25397
bench_array/1000000 324843 ns 324845 ns 2077
测试代码:
#include <benchmark/benchmark.h>
#include <bits/stdc++.h>
using namespace std;
void bench_evector2(benchmark::State& state) {
int n = state.range(0);
for (auto _: state) {
vector<int> vec;
vec.reserve(n);
for (int i = 0; i < n; i++) vec.emplace_back(i);
}
}
void bench_elist(benchmark::State& state) {
int n = state.range(0);
for (auto _: state) {
list<int> vec;
for (int i = 0; i < n; i++) vec.emplace_back(i);
}
}
void bench_evector(benchmark::State& state) {
int n = state.range(0);
vector<int> vec(n);
for (auto _: state) {
vec.clear();
for (int i = 0; i < n; i++) vec.emplace_back(i);
}
}
void bench_vector2(benchmark::State& state) {
int n = state.range(0);
for (auto _: state) {
vector<int> vec;
vec.reserve(n);
for (int i = 0; i < n; i++) vec.push_back(i);
}
}
void bench_list(benchmark::State& state) {
int n = state.range(0);
for (auto _: state) {
list<int> vec;
for (int i = 0; i < n; i++) vec.push_back(i);
}
}
void bench_vector(benchmark::State& state) {
int n = state.range(0);
vector<int> vec(n);
for (auto _: state) {
vec.clear();
for (int i = 0; i < n; i++) vec.push_back(i);
}
}
void bench_array(benchmark::State& state) {
int n = state.range(0);
vector<int> vec(n);
for (auto _: state) {
int cur = 0;
for (int i = 0; i < n; i++) vec[cur++] = i;
}
}
#define generate_test(name) BENCHMARK(bench_##name)->RangeMultiplier(10)->Range(100, 1000000);
generate_test(evector);
generate_test(evector2);
generate_test(elist);
generate_test(vector);
generate_test(vector2);
generate_test(list);
generate_test(array);
BENCHMARK_MAIN();
本文来自博客园,作者:caijianhong,转载请注明原文链接:https://www.cnblogs.com/caijianhong/p/18548753