added better test by rlblaster / ypsu / kbalazs

This commit is contained in:
Richard Thier 2021-12-13 02:28:22 +01:00
parent 1d0ba81e49
commit cc96941dca

111
ypsu.cpp Normal file
View File

@ -0,0 +1,111 @@
#include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "ska_sort.hpp"
#include "magyarsort.h"
std::map<std::string, double> results;
std::map<std::string, double> worst;
void measure(const std::string &inputtype, const std::string &name,
std::function<void()> f) {
auto begin = std::chrono::high_resolution_clock::now();
f();
auto dur = std::chrono::high_resolution_clock::now() - begin;
double seconds = dur / std::chrono::milliseconds(1) / 1000.0;
results[name] = seconds;
worst[name] = std::max(worst[name], seconds);
}
std::vector<std::string> inputtypes = {
"constant", "asc", "desc", "ascasc", "ascdesc",
"descasc", "descdesc", "smallrange", "rand",
};
std::vector<uint32_t> geninput(const std::string &type, int n) {
std::vector<uint32_t> v(n);
if (type == "constant") {
int c = rand();
for (int i = 0; i < n; i++) {
v[i] = c;
}
} else if (type == "asc") {
for (int i = 0; i < n; i++) {
v[i] = i;
}
} else if (type == "desc") {
for (int i = 0; i < n; i++) {
v[i] = n - i;
}
} else if (type == "ascasc") {
for (int i = 0; i < n / 2; i++) {
v[i] = i;
v[i + n / 2] = i;
}
} else if (type == "ascdesc") {
for (int i = 0; i < n / 2; i++) {
v[i] = i;
v[i + n / 2] = n - i;
}
} else if (type == "descasc") {
for (int i = 0; i < n / 2; i++) {
v[i] = n - i;
v[i + n / 2] = i;
}
} else if (type == "descdesc") {
for (int i = 0; i < n / 2; i++) {
v[i] = n - i;
v[i + n / 2] = n - i;
}
} else if (type == "smallrange") {
int c = rand() / 2;
for (int i = 0; i < n; i++) {
v[i] = c + rand() % 100;
}
} else if (type == "rand") {
for (int i = 0; i < n; i++) {
v[i] = rand();
}
}
return v;
}
int main(void) {
int n = 100000000;
for (auto inputtype : inputtypes) {
printf("%10s", inputtype.c_str());
fflush(stdout);
std::vector<uint32_t> v(n), w(n), expected(n);
v = geninput(inputtype, n);
measure(inputtype, "copy", [&] { w = v; });
w = v;
measure(inputtype, "std", [&] { std::sort(std::begin(w), std::end(w)); });
expected = w;
w = v;
measure(inputtype, "ska", [&] { ska_sort(std::begin(w), std::end(w)); });
w = v;
measure(inputtype, "ska_copy", [&] {
std::vector<uint32_t> buf(w.size());
if (ska_sort_copy(std::begin(w), std::end(w), std::begin(buf))) {
w.swap(buf);
}
});
w = v;
measure(inputtype, "magyar", [&] { MagyarSort::sort(&w[0], w.size()); });
assert(w == expected);
for (auto r : results) printf("%9.3fs", r.second);
puts("");
}
puts("");
printf("%10s", "worst");
for (auto w : worst) printf("%9.3fs", w.second);
puts("");
printf("%10s", "");
for (auto w : worst) printf("%10s", w.first.c_str());
puts("");
return 0;
}