I was doing some test with the move semantics after I found a way on stackoverflow to measure the elapsed time in a program and I'd like to ask something about it.
I have two classes A and B and B is derived from A:
class A
{
public:
A()
{
for (int i = 0; i < MAX; i++)
str[i] = "~~";
for (int i = 0; i < MAX; i++)
flt[i] = i;
}
std::string str[MAX];
float flt[MAX];
};
B has two constructor defined like this:
B(const A*& a)
{
for (int i = 0; i < MAX; i++)
str[i] = a->str[i];
for (int i = 0; i < MAX; i++)
flt[i] = a->flt[i];
}
B(A*&& a)
{
for (int i = 0; i < MAX; i++)
str[i] = std::move(a->str[i]);
for (int i = 0; i < MAX; i++)
flt[i] = std::move(a->flt[i]);
}
where MAX = 1000
I noticed that the difference is not that big between the two constructors in terms of speed. And if I remove the std::move
function from the second constructor the times I get are the same from the first ones.
I'm expecting that the first constructor (with the const lvalue reference as argument) will copy each element of the two arrays into the two arrays of B, whereas the second constructor (with the rvalue reference as argument) will "move" the elements of the arrays.
The story doesn't change if I compare this
B(A&& a)
{
for (int i = 0; i < MAX; i++)
str[i] = std::move(a.str[i]);
for (int i = 0; i < MAX; i++)
flt[i] = std::move(a.flt[i]);
}
with this
B(A a)
{
for (int i = 0; i < MAX; i++)
str[i] = a.str[i];
for (int i = 0; i < MAX; i++)
flt[i] = a.flt[i];
}
I have no gain using the std::move
function.
Am I missing something about the move semantics?