Created
May 20, 2014 20:36
-
-
Save Arlen/54e6e8e563db9014c03a 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 <iostream> | |
#include <random> | |
#include <vector> | |
#include <deque> | |
#include <list> | |
#include <forward_list> | |
#include <array> | |
#include <chrono> | |
#include <string> | |
#include <typeinfo> | |
#include <iomanip> | |
#if defined(__GLIBCXX__) || defined(__GLIBCPP__) | |
#include <cxxabi.h> | |
#endif // __GNUC__ | |
namespace benchmark | |
{ | |
using namespace std::chrono; | |
template <class Container> | |
class Pushback | |
{ | |
public: | |
typedef typename Container::value_type ValueType; | |
typedef Container ContainerType; | |
Pushback(unsigned n) : n(n) { } | |
void run(steady_clock::time_point& st, steady_clock::time_point& et) | |
{ | |
st = steady_clock::now(); | |
for (unsigned i = 0; i < n; ++i) { | |
data.push_back(ValueType()); | |
} | |
et = steady_clock::now(); | |
} | |
static std::string name() { return std::string("push_back"); } | |
const unsigned n; | |
private: | |
Container data; | |
}; | |
template <class Container> | |
class Pushfront | |
{ | |
public: | |
typedef typename Container::value_type ValueType; | |
typedef Container ContainerType; | |
Pushfront(unsigned n) : n(n) { } | |
void run(steady_clock::time_point& st, steady_clock::time_point& et) | |
{ | |
st = steady_clock::now(); | |
for (unsigned i = 0; i < n; ++i) { | |
data.push_front(ValueType()); | |
} | |
et = steady_clock::now(); | |
} | |
static std::string name() { return std::string("push_front"); } | |
const unsigned n; | |
private: | |
Container data; | |
}; | |
template <typename T> | |
void printInfo(std::ostream& out, const T& benchmark) | |
{ | |
std::size_t len; | |
int stat; | |
out << "Benchmarking " | |
<< T::name() | |
<< " of " | |
<< std::setw(52) | |
<< std::left | |
<< abi::__cxa_demangle( | |
typeid(typename T::ContainerType).name(), nullptr, &len, &stat) | |
<< " with " | |
<< std::setw(9) | |
<< std::left | |
<< benchmark.n | |
<< " elements (" | |
<< sizeof(typename T::ValueType) | |
<< " bytes each)"; | |
} | |
} | |
template <class B> | |
void timingOf(B&& benchmark) | |
{ | |
using std::cout; | |
using std::endl; | |
using namespace std::chrono; | |
steady_clock::time_point start, end; | |
benchmark.run(start, end); | |
printInfo(cout, benchmark); | |
cout << " took: " | |
<< duration_cast<milliseconds>(end - start).count() | |
<< "ms" << endl; | |
} | |
template <unsigned S> | |
struct T | |
{ | |
uint32_t stuff[S]; | |
}; | |
int main(int argc, char* argv[]) | |
{ | |
using std::vector; | |
using std::list; | |
using std::forward_list; | |
using std::deque; | |
using std::array; | |
using std::cout; | |
using std::endl; | |
namespace bm = benchmark; | |
typedef T<1> T1; | |
typedef T<2> T2; | |
typedef T<8> T8; | |
array<unsigned, 4> Ns { 1000000, 5000000, 10000000, 20000000 }; | |
// 4 bytes | |
for (auto n : Ns) { | |
timingOf(bm::Pushback<vector<T1>>(n)); | |
timingOf(bm::Pushback<deque<T1>>(n)); | |
timingOf(bm::Pushback<list<T1>>(n)); | |
cout << endl; | |
} | |
cout << endl; | |
for (auto n : Ns) { | |
timingOf(bm::Pushfront<list<T1>>(n)); | |
timingOf(bm::Pushfront<forward_list<T1>>(n)); | |
timingOf(bm::Pushfront<deque<T1>>(n)); | |
cout << endl; | |
} | |
cout << endl; | |
// 8 bytes | |
for (auto n : Ns) { | |
timingOf(bm::Pushback<vector<T2>>(n)); | |
timingOf(bm::Pushback<deque<T2>>(n)); | |
timingOf(bm::Pushback<list<T2>>(n)); | |
cout << endl; | |
} | |
cout << endl; | |
for (auto n : Ns) { | |
timingOf(bm::Pushfront<list<T2>>(n)); | |
timingOf(bm::Pushfront<forward_list<T2>>(n)); | |
timingOf(bm::Pushfront<deque<T2>>(n)); | |
cout << endl; | |
} | |
cout << endl; | |
// 32 bytes | |
for (auto n : Ns) { | |
timingOf(bm::Pushback<vector<T8>>(n)); | |
timingOf(bm::Pushback<deque<T8>>(n)); | |
timingOf(bm::Pushback<list<T8>>(n)); | |
cout << endl; | |
} | |
cout << endl; | |
for (auto n : Ns) { | |
timingOf(bm::Pushfront<list<T8>>(n)); | |
timingOf(bm::Pushfront<forward_list<T8>>(n)); | |
timingOf(bm::Pushfront<deque<T8>>(n)); | |
cout << endl; | |
} | |
cout << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment