Last active
August 29, 2015 14:06
-
-
Save bkuhns/86b095a3a15160dc0d44 to your computer and use it in GitHub Desktop.
Comparing the performance of sorting a large vector of moveable value types vs raw and smart pointers to that type. Written for VisualStudio 2012.
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
vector<Foobar> took: 2ms | |
vector<Foobar*> took: 71ms | |
vector<shared_ptr<Foobar>> took: 75ms |
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 <vector> | |
#include <list> | |
#include <array> | |
#include <algorithm> | |
#include <iostream> | |
#include <chrono> | |
#include <memory> | |
#include <random> | |
using namespace std; | |
using namespace std::chrono; | |
class Foobar { | |
public: | |
explicit Foobar(size_t id) | |
: id_(id) | |
{ | |
size_t n = 0; | |
generate(begin(ints_), end(ints_), [&] { return n++; }); | |
} | |
// copy ctor. | |
Foobar(const Foobar& other) | |
: ints_(other.ints_) | |
{ | |
} | |
// copy assign. | |
Foobar& operator=(const Foobar& other) | |
{ | |
ints_ = other.ints_; | |
} | |
// move ctor. | |
Foobar(Foobar&& other) | |
: ints_(move(other.ints_)) | |
{ | |
} | |
// move assign. | |
Foobar& operator=(Foobar&& other) | |
{ | |
ints_ = move(other.ints_); | |
return *this; | |
} | |
~Foobar() | |
{ | |
} | |
public: | |
friend bool operator<(const Foobar& lhs, const Foobar& rhs) | |
{ | |
return lhs.id_ < rhs.id_; | |
} | |
private: | |
explicit Foobar() /*= delete*/; | |
private: | |
size_t id_; | |
std::array<size_t, 500> ints_; | |
}; | |
template<typename Vec> | |
milliseconds testSort(Vec v) | |
{ | |
auto start = steady_clock::now(); | |
sort(begin(v), end(v)); | |
auto finish = steady_clock::now(); | |
return duration_cast<milliseconds>(finish - start); | |
} | |
template<typename Vec> | |
milliseconds testSortPtr(Vec v) | |
{ | |
auto start = steady_clock::now(); | |
sort(begin(v), end(v), [](const Vec::value_type& e1, const Vec::value_type& e2) { | |
return *e1 < *e2; | |
}); | |
auto finish = steady_clock::now(); | |
return duration_cast<milliseconds>(finish - start); | |
} | |
int main() | |
{ | |
const size_t vectorSize = 200000; | |
auto v1 = vector<Foobar>(); | |
auto v2 = vector<Foobar*>(); | |
auto v3 = vector<shared_ptr<Foobar>>(); | |
for(auto i = vectorSize; i > 0; --i) { | |
v1.emplace_back(i); | |
v2.push_back(new Foobar(i)); | |
v3.push_back(make_shared<Foobar>(i)); | |
} | |
random_device rd; | |
mt19937_64 g(rd()); | |
shuffle(begin(v1), end(v1), g); | |
shuffle(begin(v2), end(v2), g); | |
shuffle(begin(v3), end(v3), g); | |
cout << "vector<Foobar> took: " << testSort(v1).count() << "ms\n"; | |
cout << "vector<Foobar*> took: " << testSortPtr(v2).count() << "ms\n"; | |
cout << "vector<shared_ptr<Foobar>> took: " << testSortPtr(v3).count() << "ms\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment