Last active
December 18, 2015 20:38
-
-
Save shininglion/5841247 to your computer and use it in GitHub Desktop.
rvalue reference is a new scheme in C++11, which allows users to reference a "rvalue". With this new scheme, we can improve performance due to the avoidance of the unnecessary copy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <cstring> | |
#include <ctime> | |
#define DIM 256 | |
#define LIMIT 1000000 | |
using namespace std; | |
class VectorWithRvalue | |
{ | |
private: | |
const int size; | |
int *array; | |
public: | |
VectorWithRvalue(const int new_size) : size(new_size), array(new int [new_size]) {} | |
// copy constructor | |
VectorWithRvalue(const VectorWithRvalue &rhs) : size(rhs.size) | |
{ | |
// deep copy is required | |
array = new int [rhs.size]; | |
memcpy(array, rhs.array, rhs.size * sizeof(int)); | |
} | |
// move constructor | |
VectorWithRvalue(VectorWithRvalue &&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]; | |
} | |
VectorWithRvalue& operator+= (const VectorWithRvalue &rhs) | |
{ | |
for(int index=0; index<size; ++index) | |
array[index] += rhs[index]; | |
return *this; | |
} | |
VectorWithRvalue operator+ (const VectorWithRvalue &rhs) const | |
{ | |
VectorWithRvalue temp(*this); | |
temp += rhs; | |
return temp; | |
} | |
~VectorWithRvalue() { delete [] array; } | |
}; | |
VectorWithRvalue&& operator+ (VectorWithRvalue &&lhs, const VectorWithRvalue &rhs) | |
{ | |
lhs += rhs; | |
return std::move(lhs); | |
} | |
double runtime_with_move_semantic (const int iterative_limit) | |
{ | |
clock_t begin, end; | |
VectorWithRvalue 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) { | |
VectorWithRvalue 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <cstring> | |
#include <ctime> | |
#define DIM 256 | |
#define LIMIT 1000000 | |
using namespace std; | |
class Vector | |
{ | |
private: | |
const int size; | |
int *array; | |
public: | |
Vector(const int new_size) : size(new_size), array(new int [new_size]) {} | |
// copy constructor | |
Vector(const Vector &rhs) : size(rhs.size) | |
{ | |
// deep copy is required | |
array = new int [rhs.size]; | |
memcpy(array, rhs.array, rhs.size * sizeof(int)); | |
} | |
int& operator[] (const int index) | |
{ | |
return array[index]; | |
} | |
int operator[] (const int index) const | |
{ | |
return array[index]; | |
} | |
Vector& operator+= (const Vector &rhs) | |
{ | |
for(int index=0; index<size; ++index) | |
array[index] += rhs[index]; | |
return *this; | |
} | |
Vector operator+ (const Vector &rhs) const | |
{ | |
Vector temp(*this); | |
temp += rhs; | |
return temp; | |
} | |
~Vector() { delete [] array; } | |
}; | |
double runtime_without_move_semantic (const int iterative_limit) | |
{ | |
clock_t begin, end; | |
Vector 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) { | |
Vector 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_without_move_semantic(LIMIT) << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment