Created
September 16, 2010 15:07
-
-
Save kikairoya/582577 to your computer and use it in GitHub Desktop.
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 <algorithm> | |
#include <array> | |
#include <chrono> | |
#include <functional> | |
#include <iostream> | |
#include <memory> | |
#include <numeric> | |
#include <random> | |
#include <vector> | |
#include <boost/iterator/indirect_iterator.hpp> | |
template <typename TimeT = std::chrono::milliseconds, typename F> | |
inline TimeT take_time(F &&f) { | |
const auto begin = std::chrono::system_clock::now(); | |
f(); | |
const auto end = std::chrono::system_clock::now(); | |
return std::chrono::duration_cast<TimeT>(end - begin); | |
} | |
template <typename F> | |
inline void bench(const std::string &s, F &&f) { | |
const auto t = take_time<>(std::forward<F>(f)); | |
std::cout << s << t.count() << std::endl; | |
} | |
struct S { | |
struct impl { | |
impl(int i): v(i) { } | |
int v; | |
}; | |
S(): pimpl(new impl(0)) { } | |
S(int i): pimpl(new impl(i)) { } | |
S(const S &s): pimpl(new impl(*s.pimpl)) { } | |
S &operator =(const S &s) { S(s).swap(*this); return *this; } | |
void swap(S &other) { impl *p(other.pimpl); other.pimpl = pimpl; pimpl = p; } | |
~S() { delete pimpl; } | |
int get() const { return pimpl->v; } | |
int operator *() const { return get(); } | |
S &operator +=(const S &v) { pimpl->v += v.get(); return *this; } | |
friend S operator +(S x, const S &y) { return x += y; } | |
impl *pimpl; | |
}; | |
const int copy_size = 10000000; | |
const size_t seed_length = 16; | |
int main() { | |
using namespace std; | |
using namespace boost; | |
vector<int> src; | |
function<int ()> rnd; | |
{ | |
random_device rnd_dev; | |
array<uint_least32_t, seed_length> seed; | |
generate(seed.begin(), seed.end(), ref(rnd_dev)); | |
seed_seq seq(seed.begin(), seed.end()); | |
rnd = bind(uniform_int_distribution<>(), mt19937(seq)); | |
generate(src.begin(), src.end(), ref(rnd)); | |
} | |
{ | |
vector<int *> v1, v2; | |
v1.reserve(copy_size); | |
v2.reserve(copy_size); | |
bench("generate int *: ", [&] { | |
generate_n(back_inserter(v1), copy_size, [&] { return new int(rnd()); }); | |
}); | |
bench("copy int *:", [&] { | |
copy(v1.begin(), v1.end(), back_inserter(v2)); | |
}); | |
bench("accumulate int *:", [&] { | |
cout << accumulate(indirect_iterator<decltype(v1.begin())>(v1.begin()), indirect_iterator<decltype(v1.end())>(v1.end()), 0) << endl; | |
}); | |
bench("delete int *:", [&] { | |
for_each(v2.begin(), v2.end(), [](int *p) { delete p; }); | |
}); | |
} | |
{ | |
vector<shared_ptr<int>> v1, v2; | |
v1.reserve(copy_size); | |
v2.reserve(copy_size); | |
bench("generate shared_ptr<int>: ", [&] { | |
generate_n(back_inserter(v1), copy_size, [&] { return shared_ptr<int>(new int(rnd())); }); | |
}); | |
bench("copy shared_ptr<int>:", [&] { | |
copy(v1.begin(), v1.end(), back_inserter(v2)); | |
}); | |
bench("accumulate share_ptr<int>:", [&] { | |
cout << accumulate(indirect_iterator<decltype(v1.begin())>(v1.begin()), indirect_iterator<decltype(v1.end())>(v1.end()), 0) << endl; | |
}); | |
bench("delete shared_ptr<int>:", [&] { v2.clear(); }); | |
} | |
{ | |
vector<S *> v1, v2; | |
v1.reserve(copy_size); | |
v2.reserve(copy_size); | |
bench("generate S *: ", [&] { | |
generate_n(back_inserter(v1), copy_size, [&] { return new S(rnd()); }); | |
}); | |
bench("copy S *:", [&] { | |
copy(v1.begin(), v1.end(), back_inserter(v2)); | |
}); | |
bench("accumulate S *:", [&] { | |
cout << accumulate(indirect_iterator<decltype(v1.begin())>(v1.begin()), indirect_iterator<decltype(v1.end())>(v1.end()), S()).get() << endl; | |
}); | |
bench("delete S *:", [&] { | |
for_each(v2.begin(), v2.end(), [](S *p) { delete p; }); | |
}); | |
} | |
} | |
// output | |
//generate int *: 3508 | |
//copy int *:62 | |
//-502118182 | |
//accumulate int *:38 | |
//delete int *:974 | |
//generate shared_ptr<int>: 6589 | |
//copy shared_ptr<int>:228 | |
//1463182742 | |
//accumulate share_ptr<int>:75 | |
//delete shared_ptr<int>:165 | |
//generate S *: 6267 | |
//copy S *:50 | |
//-549355879 | |
//accumulate S *:11580 | |
//delete S *:1902 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment