#include <iostream>
#include <algorithm>
#include <cstring>
#include <ctime>
#define DIM 256
#define LIMIT 1000000
using namespace std;
class VectorWithMove
{
private:
const int size;
int *array;
public:
VectorWithMove(const int new_size) : size(new_size), array(new int [new_size]) {}
// copy constructor
VectorWithMove(const VectorWithMove &rhs) : size(rhs.size)
{
// deep copy is required
array = new int [rhs.size];
memcpy(array, rhs.array, rhs.size * sizeof(int));
}
// move constructor
VectorWithMove(VectorWithMove &&rhs) : size(rhs.size), array(rhs.array)
{
rhs.array = nullptr;
}
int& operator[] (const int index)
{
return array[index];
}
int operator[] (const int index) const
{
return array[index];
}
VectorWithMove& operator+= (const VectorWithMove &rhs)
{
for(int index=0; index<size; ++index)
array[index] += rhs[index];
return *this;
}
VectorWithMove operator+ (const VectorWithMove &rhs) const
{
VectorWithMove temp(*this);
temp += rhs;
return temp;
}
~VectorWithMove() { delete [] array; }
};
VectorWithMove&& operator+ (VectorWithMove &&lhs, const VectorWithMove &rhs)
{
lhs += rhs;
return std::move(lhs);
}
double runtime_with_move_semantic (const int iterative_limit)
{
clock_t begin, end;
VectorWithMove a(DIM), b(DIM), c(DIM);
for(int i=0; i<DIM; ++i) {
a[i] = 0;
b[i] = 1;
c[i] = 2;
}
begin = clock();
for (int iter; iter<iterative_limit; ++iter) {
VectorWithMove d = a+b+c;
d[iter&DIM] = 2;
}
end = clock();
return (static_cast<double>(end-begin) / static_cast<double>(CLOCKS_PER_SEC) );
}
int main()
{
cout << runtime_with_move_semantic(LIMIT) << '\n';
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGN0aW1lPgojZGVmaW5lIERJTSAyNTYKI2RlZmluZSBMSU1JVCAxMDAwMDAwCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgVmVjdG9yV2l0aE1vdmUKewpwcml2YXRlOgogICAgY29uc3QgaW50IHNpemU7Cglpb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