Skip to content

Commit

Permalink
Add benchmarks for reversed and sorted sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuji Goro committed Oct 19, 2011
1 parent 4f28f04 commit 97f9f42
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 18 deletions.
51 changes: 42 additions & 9 deletions README.md
Expand Up @@ -9,18 +9,51 @@ BENCHMARK
bench.cpp, invoked by `make bench`, is a simple benchmark.
An example output is (scale: sec.):

RANDOMIZED SEQUENCE
int
size 100000
std::sort 0.631852
std::stable_sort 0.846656
timsort 1.1865
std::sort 0.64711
std::stable_sort 0.858373
timsort 1.19961
double
size 100000
std::sort 0.759048
std::stable_sort 1.04307
timsort 1.30662
std::sort 0.764102
std::stable_sort 1.01592
timsort 1.35223
boost::rational
size 100000
std::sort 5.94573
std::stable_sort 5.41838
timsort 5.33095
std::sort 5.76009
std::stable_sort 5.35419
timsort 5.52264
REVERSED SEQUENCE
int
size 100000
std::sort 0.129067
std::stable_sort 0.290258
timsort 0.029547
double
size 100000
std::sort 0.145228
std::stable_sort 0.281365
timsort 0.023615
boost::rational
size 100000
std::sort 3.66021
std::stable_sort 2.36177
timsort 0.280779
SORTED SEQUENCE
int
size 100000
std::sort 0.113937
std::stable_sort 0.183452
timsort 0.012675
double
size 100000
std::sort 0.121672
std::stable_sort 0.242327
timsort 0.020005
boost::rational
size 100000
std::sort 3.87167
std::stable_sort 2.78654
timsort 0.302464
45 changes: 36 additions & 9 deletions bench.cpp
Expand Up @@ -9,9 +9,12 @@

#include "timsort.hpp"

enum state_t {
sorted, randomized, reversed
};

template <typename value_t>
void bench(int const size) {
static void bench(int const size, state_t const state) {
std::cerr << "size\t" << size << std::endl;

std::less<value_t> lt;
Expand All @@ -21,7 +24,20 @@ void bench(int const size) {
a.push_back((i+1) * 10);
}

std::random_shuffle(a.begin(), a.end());
switch(state) {
case randomized:
std::random_shuffle(a.begin(), a.end());
break;
case reversed:
std::stable_sort(a.begin(), a.end(), lt);
std::reverse(a.begin(), a.end());
break;
case sorted:
std::stable_sort(a.begin(), a.end(), lt);
break;
default:
assert(!"not reached");
}

{
std::vector<value_t> b(a);
Expand Down Expand Up @@ -62,17 +78,28 @@ void bench(int const size) {
}
}

static void doit(int const n, state_t const state) {
std::cerr << "int" << std::endl;
bench<int>(n, state);

std::cerr << "double" << std::endl;
bench<double>(n, state);

std::cerr << "boost::rational" << std::endl;
bench< boost::rational<long long> >(n, state);
}

int main() {
typedef boost::rational<int> value_t;
const int N = 100 * 1000;

std::srand(std::time(NULL));

std::cerr << "int" << std::endl;
bench<int>(100 * 1000);
std::cerr << "RANDOMIZED SEQUENCE" << std::endl;
doit(N, randomized);

std::cerr << "double" << std::endl;
bench<double>(100 * 1000);
std::cerr << "REVERSED SEQUENCE" << std::endl;
doit(N, reversed);

std::cerr << "boost::rational" << std::endl;
bench< boost::rational<long long> >(100 * 1000);
std::cerr << "SORTED SEQUENCE" << std::endl;
doit(N, sorted);
}

0 comments on commit 97f9f42

Please sign in to comment.