Created
January 13, 2015 15:58
-
-
Save cristicbz/8a2ed587d1a7a6740d97 to your computer and use it in GitHub Desktop.
RV/V Move ctors
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 <cstddef> | |
#include <cstdlib> | |
#include <ctime> | |
#include <iostream> | |
#include <vector> | |
struct ByValue { | |
ByValue(std::vector<int> a, std::vector<int> b) | |
: a_{std::move(a)}, b_{std::move(b)} {} | |
std::vector<int> a_; | |
std::vector<int> b_; | |
}; | |
struct ByRValue { | |
ByRValue(std::vector<int> &&a, std::vector<int> &&b) : a_{a}, b_{b} {} | |
std::vector<int> a_; | |
std::vector<int> b_; | |
}; | |
template<class T> | |
void bench(size_t n) { | |
std::vector<int> a, b; | |
int sum = 0; | |
for (size_t i = 0; i < n; ++i) { | |
a.clear(); | |
b.clear(); | |
for (int j = 0; j < 2048; ++j) { | |
a.push_back(j); | |
b.push_back(j); | |
} | |
auto z = T{std::move(a), std::move(b)}; | |
for (int i : z.a_) sum += i; | |
for (int i : z.b_) sum += i; | |
} | |
std::cout << "Sum: " << sum << std::endl; | |
} | |
int main(int argc, char *argv[]) { | |
constexpr size_t B = 10000; | |
constexpr size_t N = 10000000; | |
std::cout << "ByRValue: Burn in..." << std::endl; | |
bench<ByRValue>(B); | |
std::cout << "ByRValue: Measuring..." << std::endl; | |
auto t0 = clock(); | |
bench<ByRValue>(N); | |
auto dt = static_cast<double>(clock() - t0) / CLOCKS_PER_SEC; | |
std::cout << "ByRValue: " << (dt / N * 1e9) << "ns per iteration." << std::endl; | |
std::cout << "ByValue: Burn in..." << std::endl; | |
bench<ByValue>(B); | |
std::cout << "ByValue: Measuring..." << std::endl; | |
t0 = clock(); | |
bench<ByValue>(N); | |
dt = static_cast<double>(clock() - t0) / CLOCKS_PER_SEC; | |
std::cout << "ByValue: " << (dt / N * 1e9) << "ns per iteration." << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment