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