-
-
Save hongxuchen/4b7dc302bbb38daf601d to your computer and use it in GitHub Desktop.
move semantics issue
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
# for i in `seq 1 5`; do ./a.out>/dev/null;echo;done | |
### using std::vector<int> | |
7995 | |
9732 | |
8003 | |
7981 | |
7982 | |
9833 | |
8124 | |
8070 | |
8137 | |
10157 | |
8075 | |
7987 | |
8032 | |
9821 | |
8046 | |
8040 | |
8071 | |
9782 | |
8211 | |
8064 | |
### using MyVector | |
8327 | |
10450 | |
8250 | |
8706 | |
8120 | |
9988 | |
8309 | |
8102 | |
8466 | |
10372 | |
8772 | |
8453 | |
8589 | |
10348 | |
8386 | |
8134 | |
8301 | |
10779 | |
8121 | |
8681 |
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 <vector> | |
#include <chrono> | |
#define DurationTy std::chrono::duration_cast<std::chrono::milliseconds> | |
size_t const MAX = 100000u; | |
size_t const NUM = MAX / 100; | |
template <class T> | |
class MyVector { | |
private: | |
std::vector<T> _vec; | |
public: | |
MyVector() : _vec() {} | |
MyVector(const MyVector& other) : _vec(other._vec) {} | |
// MyVector& operator=(const MyVector& other) { | |
// if (this != &other) this->_vec = other._vec; | |
// return *this; | |
// } | |
typename std::vector<T>::const_iterator begin() { return _vec.begin(); } | |
typename std::vector<T>::const_iterator end() { return _vec.end(); } | |
void push_back(T t) { this->_vec.push_back(t); } | |
}; | |
// typedef MyVector<int> VectTy; | |
typedef std::vector<int> VectTy; | |
int randomize(int mod) { return std::rand() % mod; } | |
VectTy factory(size_t size, bool pos) { | |
VectTy vect; | |
if (pos) { | |
for (size_t i = 0u; i < size; i++) { | |
// vect.push_back(i); | |
vect.push_back(randomize(size)); | |
} | |
} else { | |
for (size_t i = 0u; i < size * 2; i++) { | |
// vect.push_back(i); | |
vect.push_back(randomize(size)); | |
} | |
} | |
return vect; | |
} | |
long d1(VectTy vect) { | |
long sum = 0; | |
for (auto& v : vect) sum += v; | |
return sum; | |
} | |
long d2(VectTy& vect) { | |
long sum = 0; | |
for (auto& v : vect) sum += v; | |
return sum; | |
} | |
long d3(VectTy&& vect) { | |
long sum = 0; | |
for (auto& v : vect) sum += v; | |
return sum; | |
} | |
int main(void) { | |
{ | |
auto start = std::chrono::steady_clock::now(); | |
long total = 0; | |
for (size_t i = 0u; i < NUM; ++i) { | |
total += d1(factory(MAX, i % 2)); // T1 | |
} | |
auto end = std::chrono::steady_clock::now(); | |
std::cout << total << std::endl; | |
auto elapsed = DurationTy(end - start); | |
std::cerr << elapsed.count() << std::endl; | |
} | |
{ | |
auto start = std::chrono::steady_clock::now(); | |
long total = 0; | |
for (size_t i = 0u; i < NUM; ++i) { | |
VectTy vect = factory(MAX, i % 2); // T2 | |
total += d1(vect); | |
} | |
auto end = std::chrono::steady_clock::now(); | |
std::cout << total << std::endl; | |
auto elapsed = DurationTy(end - start); | |
std::cerr << elapsed.count() << std::endl; | |
} | |
{ | |
auto start = std::chrono::steady_clock::now(); | |
long total = 0; | |
for (size_t i = 0u; i < NUM; ++i) { | |
VectTy vect = factory(MAX, i % 2); // T3 | |
total += d2(vect); | |
} | |
auto end = std::chrono::steady_clock::now(); | |
std::cout << total << std::endl; | |
auto elapsed = DurationTy(end - start); | |
std::cerr << elapsed.count() << std::endl; | |
} | |
{ | |
auto start = std::chrono::steady_clock::now(); | |
long total = 0; | |
for (size_t i = 0u; i < NUM; ++i) { | |
total += d3(factory(MAX, i % 2)); // T3 | |
} | |
auto end = std::chrono::steady_clock::now(); | |
std::cout << total << std::endl; | |
auto elapsed = DurationTy(end - start); | |
std::cerr << elapsed.count() << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment