C++ examples for Class:Constructor
Comparing Copy Constructors with Move Constructors
#include <chrono> #include <iostream> #include <string> #include <vector> using namespace std; using namespace chrono; using namespace literals; class MyClass/*ww w . j av a2 s . co m*/ { private: vector<string> m_String{" copy constructed!"s}; int m_Value{ 1 }; public: MyClass() = default; MyClass(const MyClass& rhs) = default; MyClass(MyClass&& rhs) = default; int GetValue() const { return m_Value; } }; int main() { using MyVector = vector<MyClass>; constexpr unsigned int ITERATIONS{ 1000000U }; MyVector copyConstructed(ITERATIONS); int value{ 0 }; auto copyStartTime = high_resolution_clock::now(); for (unsigned int i=0; i < ITERATIONS; ++i) { MyClass myClass; copyConstructed.push_back(myClass); value = myClass.GetValue(); } auto copyEndTime = high_resolution_clock::now(); MyVector moveConstructed(ITERATIONS); auto moveStartTime = high_resolution_clock::now(); for (unsigned int i=0; i < ITERATIONS; ++i) { MyClass myClass; moveConstructed.push_back(move(myClass)); value = myClass.GetValue(); } auto moveEndTime = high_resolution_clock::now(); cout << value << endl; auto copyDuration = duration_cast<milliseconds>(copyEndTime - copyStartTime); cout << "Copy lasted: " << copyDuration.count() << "ms" << endl; auto moveDuration = duration_cast<milliseconds>(moveEndTime - moveStartTime); cout << "Move lasted: " << moveDuration.count() << "ms" << endl; return 0; }