Benchmarking smart pointers

63 views
Skip to first unread message

Siddesh Parmar

unread,
Sep 28, 2022, 7:34:38 AM9/28/22
to benchmark-discuss
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
                                               

dominic hamon

unread,
Sep 28, 2022, 8:29:31 AM9/28/22
to Siddesh Parmar, benchmark-discuss
i don't have any a priori expectations about what would be faster or slower, and i assume that you're running these benchmarks because you don't either.  given the results you have, you have two choices:

1. accept the results
2. don't accept the results

if you go with #1 we're done here :)

it sounds like you're going with #2 in which case i suggest you check the generated assembly for starters to see why it might be slower or faster.


i'm not sure if there's anything useful there :)



dominic hamon | google
there are no bad ideas; only good ideas that go horribly wrong.


--
You received this message because you are subscribed to the Google Groups "benchmark-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to benchmark-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/benchmark-discuss/5dba5448-706e-42be-9013-4c404be317ccn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages