Hi All,
I wrote 3 cases as shown below
1) creating shared_ptr using new
2) Creating shared_ptr using make_shared
3) Creating unique_ptr using make_unique
static void benchmark_shared(benchmark::State &s)
{
for ( auto _ : s )
{
std::shared_ptr<int> ptr(new int (10));
benchmark::DoNotOptimize(ptr);
ptr.reset();
}
}
static void benchmark_make_shared(benchmark::State &s)
{
for ( auto _ : s )
{
std::shared_ptr<int> ptr = std::make_shared<int>(10);
benchmark::DoNotOptimize(ptr);
ptr.reset();
}
}
static void benchmark_make_unique(benchmark::State &s)
{
for ( auto _ : s )
{
std::unique_ptr<int> ptr = std::make_unique<int>(10);
benchmark::DoNotOptimize(ptr);
ptr.reset();
}
}
Below is the output
2022-09-28T17:00:04+05:30
Running ./test
Run on (12 X 2994.37 MHz CPU s)
CPU Caches:
L1 Data 32 KiB (x6)
L1 Instruction 32 KiB (x6)
L2 Unified 512 KiB (x6)
L3 Unified 4096 KiB (x1)
Load Average: 0.00, 0.01, 0.00
----------------------------------------------------------------
Benchmark Time CPU Iterations
----------------------------------------------------------------
benchmark_shared 70.4 ns 70.4 ns 9875275
benchmark_make_shared 165 ns 165 ns 4336108
benchmark_make_unique 75.9 ns 75.9 ns 9254510
as much i am aware calling shared_ptr using new should have worst performance right ?
Any help /advice /suggestions are appreciated.
Thanks in advance
Thanks,
Siddesh