Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6762053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:16:04+00:00 2026-05-26T14:16:04+00:00

I am trying to compare performance between raw pointers, boost shared_ptr and boost weak_ptr.

  • 0

I am trying to compare performance between raw pointers, boost shared_ptr and boost weak_ptr. On the dereferencing part, I expected shared_ptr and raw_ptr to be equal, but results show that shared_ptr is about twice as slow. For the test, I am creating an array with either pointers or shared pointers to ints, and then dereferencing in a loop like this:

int result;
for(int i = 0; i != 100; ++i)
{
  for(int i = 0; i != SIZE; ++i)
    result += *array[i];
}

The full code for the test can be found here:
https://github.com/coolfluid/coolfluid3/blob/master/test/common/utest-ptr-benchmark.cpp

Test timings for an optimized build without assertions can be found here:
http://coolfluidsrv.vki.ac.be/cdash/testDetails.php?test=145592&build=7777

The values of interest are “DerefShared time” and “DerefRaw time”

I guess the test may be flawed somehow, but I failed to figure out where the difference comes from. Profiling shows the operator* from shared_ptr gets inlined, it just seems to take more time. I double-checked that the boost assertion is off.

I would be very grateful if anyone can explain where the difference might come from.

Additional stand-alone test:
https://gist.github.com/1335014

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:16:05+00:00Added an answer on May 26, 2026 at 2:16 pm

    As Alan Stokes said in his comment, this is due to cache effects. Shared Pointers include a reference count, which means they are physically larger in memory than a raw pointer. When stored in a contiguous array, you get less pointers per cache line, which means the loop has to go out to main memory more often than it would for a raw pointer.

    You can observe this behavior by, in your raw pointer test, allocating SIZE*2 ints, but also changing the de-reference loop to stride by i+=2 instead of ++i. Doing this yielded approximately the same results in my tests. My code for the raw test is below.

    #include <iostream>
    #include <boost/timer.hpp>
    
    #define SIZE 1000000
    
    typedef int* PtrT;
    
    int do_deref(PtrT* array)
    {
        int result = 0;
        for(int i = 0; i != 1000; ++i)
        {
            for(int i = 0; i != SIZE*2; i+=2)
                result += *array[i];
        }
    
        return result;
    }
    
    int main(void)
    {
        PtrT* array = new PtrT[SIZE*2];
        for(int i = 0; i != SIZE*2; ++i)
            array[i] = new int(i);
        boost::timer timer;
        int result = do_deref(array);
        std::cout << "deref took " << timer.elapsed() << "s" << std::endl;
        return result;
    }
    

    Incidentally, using boost::make_shared<int>(i) instead of PtrT(new int(I))allocates the reference count and the object together in memory rather than in separate locations. In my tests this improved the performance of shared pointer dereference by about 10-20%. Code for that is below:

    #include <iostream>
    #include <boost/timer.hpp>
    #include <boost/shared_ptr.hpp>
    #include <boost/make_shared.hpp>
    #define SIZE 1000000
    
    typedef boost::shared_ptr<int> PtrT;
    
    int do_deref(PtrT* array)
    {
        int result = 0;
        for(int j = 0; j != 1000; ++j)
        {
            for(int i = 0; i != SIZE; ++i)
                result += *array[i];
        }
    
        return result;
    }
    
    int main(void)
    {
        PtrT* array = new PtrT[SIZE];
        for(int i = 0; i != SIZE; ++i)
            array[i] = boost::make_shared<int>(i);
        boost::timer timer;
        int result = do_deref(array);
        std::cout << "deref took " << timer.elapsed() << "s" << std::endl;
        return result;
    }
    

    My Results (x86-64 Unbuntu 11 VM):

    Original Raw: 6.93
    New Raw: 12.9
    Original Shared: 12.7
    New Shared: 10.59
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to compare the performance of boost::multi_array to native dynamically allocated arrays,
I'm trying to compare files between two labels with cleartool but I can't seem
I am trying to compare the performance of several Javascript libraries. Although measuring transaction
Im trying to compare these two chars but on win 32 Visual Studio 2008:
I've been trying to compare the memory footprint between a VB6 application and .Net
I'm trying to get a feel for the difference in performance between integer multiplication
I'm trying to compare GPU to CPU performance. For the NVIDIA GPU I've been
I am trying to compare the performance of a specific F# benchmark running on
I trying to compare the styles of elements on my page. But one element
I am trying to see if the performance of both can be compared based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.