Skip to content

Instantly share code, notes, and snippets.

@mcobzarenco
Created January 13, 2015 15:49
Show Gist options
  • Save mcobzarenco/c5eddd824c2e9cc54359 to your computer and use it in GitHub Desktop.
Save mcobzarenco/c5eddd824c2e9cc54359 to your computer and use it in GitHub Desktop.
#include <chrono>
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
uint64_t time_it_us(const T& t) {
auto begin = std::chrono::high_resolution_clock::now();
t();
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>
(end-begin).count();
}
struct A {
A(std::vector<int> v) : v_{std::move(v)} {}
std::vector<int> v_;
};
struct B {
// B(const std::vector<int>& v) : v_{v} {}
B(std::vector<int>&& v) : v_{v} {}
std::vector<int> v_;
};
int main() {
std::vector<int> v1, v2;
for (int i = 0; i < 1000000; ++i) {
v1.push_back(i);
v2.push_back(i);
}
auto by_val = [&v1] () {
A a{std::move(v1)};
};
auto by_uref = [&v2] () {
B b{std::move(v2)};
};
std::cout << "by val: " << time_it_us(by_val) << "\n";
std::cout << "by uref: " << time_it_us(by_uref) << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment