1 Star 0 Fork 0

ChampagneW / CppConcurrent

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
inline_bench.cpp 3.66 KB
Copy Edit Raw Blame History
hswsp authored 2026-10-28 12:57 +08:00 . Add README
// inline_bench.cpp —— "小对象内联存储"(SSO / small_vector 的思路)值不值
//
// 编译:g++ -std=c++17 -O2 -pthread inline_bench.cpp -o inline_bench
// 用法:./inline_bench [元素个数] [每元素字节数] [重复次数]
//
// 对比两种 vector 形态:
// HeapVec :永远在堆上分配(std::vector 的行为)
// InlineVec:前 N 字节内联在对象里,超出才去堆(folly::small_vector 的思路)
//
// 通过重载 operator new / delete 统计分配次数与字节数,
// 让"省了几次分配"这件事可量化。
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <new>
#include <vector>
using namespace std::chrono;
// ---------- 分配计数器 ----------
static long g_alloc_count = 0, g_free_count = 0;
static size_t g_alloc_bytes = 0;
static bool g_watch = false;
void *operator new(size_t n) {
if (g_watch) { ++g_alloc_count; g_alloc_bytes += n; }
void *p = std::malloc(n ? n : 1);
if (!p) throw std::bad_alloc();
return p;
}
void operator delete(void *p) noexcept { if (g_watch && p) ++g_free_count; std::free(p); }
void operator delete(void *p, size_t) noexcept { if (g_watch && p) ++g_free_count; std::free(p); }
// ---------- A. 总是堆分配 ----------
template <typename T>
struct HeapVec {
T *p = nullptr; size_t n = 0;
explicit HeapVec(size_t k) : n(k) { p = new T[k]; } // 走 operator new[],可被计数
~HeapVec() { delete[] p; }
T &operator[](size_t i) { return p[i]; }
};
// ---------- B. 前 InlineN 个元素内联 ----------
template <typename T, size_t InlineN>
struct InlineVec {
static constexpr size_t kBuf = sizeof(T) * InlineN;
alignas(T) char buf[kBuf];
T *heap = nullptr;
size_t n = 0;
explicit InlineVec(size_t k) : n(k) {
if (k > InlineN) heap = new T[k]; // 只有超容量才分配
}
~InlineVec() { if (heap) delete[] heap; }
T *data() { return heap ? heap : reinterpret_cast<T *>(buf); }
T &operator[](size_t i) { return data()[i]; }
};
int main(int argc, char **argv) {
size_t K = argc > 1 ? (size_t)std::atol(argv[1]) : 8; // 每个容器的元素数
int reps = argc > 3 ? std::atoi(argv[3]) : 5;
const long N = 1000000; // 容器个数
std::printf("每个容器 %zu 个 int64 共 %ld 个容器 重复 %d 取最小值\n\n", K, N, reps);
// 容器对象本身都放在**栈**上,只统计"数据缓冲区"的分配次数。
// 这样比较的才是内联存储本身的效果,而不是容器对象大小的差异。
auto run = [&](const char *tag, auto &&body) {
double best = 1e30;
long ac = 0, ab = 0;
for (int r = 0; r < reps; ++r) {
g_alloc_count = 0; g_alloc_bytes = 0; g_free_count = 0; g_watch = true;
auto t0 = steady_clock::now();
long s = 0;
body(s);
double ms = duration<double, std::milli>(steady_clock::now() - t0).count();
g_watch = false;
if (ms < best) { best = ms; ac = g_alloc_count; ab = (long)g_alloc_bytes; }
}
std::printf("%-34s %9.2f ms 堆分配 %8ld 次 %9ld 字节\n", tag, best, ac, ab);
return best;
};
double a = run("A 总是堆分配 (std::vector 型)", [&](long &s) {
for (long i = 0; i < N; ++i) { HeapVec<long> v(K); v[0] = i; s += v[0] + v[K-1]; }
});
double b = run("B 内联 8 元素 (small_vector 型)", [&](long &s) {
for (long i = 0; i < N; ++i) { InlineVec<long, 8> v(K); v[0] = i; s += v[0] + v[K-1]; }
});
std::printf("\n 内联相对堆分配: %.2fx\n", a / b);
std::printf(" 注:加速比在单次分配极快的场景下会被噪声淹没;\n");
std::printf(" 分配次数与字节数的差异才是确定性的结论。\n\n");
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/champagnew/cpp-concurrent.git
git@gitee.com:champagnew/cpp-concurrent.git
champagnew
cpp-concurrent
CppConcurrent
master

Search