I have a method
structA { shared_ptr<B> m_b; // 2 options to set m_b void setB1(shared_ptr<B> b) { m_b = move(b); } void setB2(shared_ptr<B> const&b) { m_b = b; } }; Which one is better in performance? They both do copies if I call setB1 as setB1(b) other than setB1(move(b)). I am more concerned about its performance when b can be nullified and b can only be copied.
My Testing Answers on VC2015:
setB1 is faster than setB2
- by 30% for lvalue b
- by 8% for rvalue b
m_bto beshared_ptr<B>and not justB.shared_ptrpointing to the same thing, moving nullifies originalshared_ptr. Rather than about performance, you should think about what your goal is.bobject which is destroyed upon function exit anyway.setB1assetB1(b)other thansetB1(move(b)). I am more concerned about its performance, for example when b can be nullified.