Hi,
I'm kind of new to Google benchmark but I was trying to run some simple benchmarks comparing performance for construction of unique_ptr, shared_ptr, and raw pointers to an object of MyClass. I'm wondering why the times for raw pointers are slightly greater than those of unique_ptrs. I would expect the opposite to be true. Is there anything wrong with my implementation or something else I'm missing?
--------------------------------------------------------------------
Benchmark Time CPU Iterations
--------------------------------------------------------------------
BM_UniquePtr_MyClass/1 13.2 ns 13.2 ns 52478142
BM_UniquePtr_MyClass/10 140 ns 140 ns 4999401
BM_UniquePtr_MyClass/100 1300 ns 1300 ns 532017
BM_UniquePtr_MyClass/1000 12893 ns 12893 ns 53916
BM_SharedPtr_MyClass/1 36.2 ns 36.2 ns 19350467
BM_SharedPtr_MyClass/10 353 ns 353 ns 1983883
BM_SharedPtr_MyClass/100 3548 ns 3548 ns 197663
BM_SharedPtr_MyClass/1000 35447 ns 35447 ns 19803
BM_RawPtr_MyClass/1 13.9 ns 13.9 ns 50482698
BM_RawPtr_MyClass/10 144 ns 144 ns 4846621
BM_RawPtr_MyClass/100 1336 ns 1336 ns 521874
BM_RawPtr_MyClass/1000 13323 ns 13323 ns 52649
class MyClass {
int i = 0;
double d = 1.1;
char c = 'c';
bool b = false;
};
static void BM_UniquePtr_MyClass(benchmark::State &state) {
for (auto _ : state) {
for (auto i = 0; i < state.range(0); i++) {
std::unique_ptr<MyClass> m(new MyClass);
benchmark::DoNotOptimize(m);
m.reset();
}
}
}
static void BM_SharedPtr_MyClass(benchmark::State &state) {
for (auto _ : state) {
for (auto i = 0; i < state.range(0); i++) {
std::shared_ptr<MyClass> m(new MyClass);
benchmark::DoNotOptimize(m);
m.reset();
}
}
}
static void BM_RawPtr_MyClass(benchmark::State &state) {
for (auto _ : state) {
for (auto i = 0; i < state.range(0); i++) {
MyClass* m(new MyClass);
benchmark::DoNotOptimize(m);
delete m;
}
}
}
BENCHMARK(BM_UniquePtr_MyClass)->RangeMultiplier(10)->Range(1, 1000);
BENCHMARK(BM_SharedPtr_MyClass)->RangeMultiplier(10)->Range(1, 1000);
BENCHMARK(BM_RawPtr_MyClass)->RangeMultiplier(10)->Range(1, 1000);