this code
#include <iostream>
#include <vector>
struct obj
{
std::string name;
int age;
float money;
obj():name("NO_NAME"),age(0),money(0.0f){}
obj(const std::string& _name, const int& _age, const float& _money):name(_name),age(_age),money(_money){}
obj(obj&& tmp): name(tmp.name), age(tmp.age), money(tmp.money) {}
obj& operator=(obj&&) {return *this;}
};
int main(int argc, char* argv[])
{
std::vector<obj> v;
for( int i = 0; i < 5000000; ++i )
{
v.emplace_back(obj("Jon", 45, 500.6f));
}
return(0);
}
is about 2 time slower than the equivalent with v.push_back(obj("Jon", 45, 500.6f));
and I don't get why.
I have tested this with bot g++ 4.7.2
and clang++ 3.3
.
Where I'm wrong ?
now that i have corrected my move construnctor i will add more
this is the emplace_back version
I'm testing this 2 with the time
utility under Linux and compiling them with
g++-4.7 -std=c++11 -s -O3 -DNDEBUG
or
clang++ -std=c++11 -s -O3 -DNDEBUG