#include #include #include #include #include #include #include #include #include #include #include #include #include "ska_sort.hpp" #include "magyarsort.h" std::map results; std::map worst; void measure(const std::string &inputtype, const std::string &name, std::function 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 inputtypes = { "constant", "asc", "desc", "ascasc", "ascdesc", "descasc", "descdesc", "smallrange", "rand", }; std::vector geninput(const std::string &type, int n) { std::vector 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 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 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; }