magyarsort/test.cpp

181 lines
4.4 KiB
C++
Raw Normal View History

2021-03-11 21:22:37 +01:00
/* LICENCE: CC3 - look it up, you need to mention me but that is all */
/* CONFIG */
// Uncomment next line to follow Creel: https://www.youtube.com/watch?v=ujb2CIWE8zY
// #define CREEL // Overwrites TEST_LEN to 16 and sets MAGYAR_SORT_NIBBLE!
// Number of input elements to generate - unused when CREEL is defined!
//#define SORT_WIDTH 200000000
2021-12-14 17:29:33 +01:00
#define SORT_WIDTH 40000000
// Uncomment this to use nibbles as digits and not bytes - CREEL defines this anyways
//#define MAGYAR_SORT_NIBBLE
// Uncomment if you want to see output before / after sorts (debugging for example)
//#define PRINT_OUTPUT
2021-12-14 17:29:33 +01:00
//#define SKA_SORT
// Uncomment for perf / cachegring and similar runs!
#define MEASURE_ONLY
2021-12-14 17:29:33 +01:00
// Uncomment this for performance measuring with "coz"
// XXX: Beware that we will do this many times of sorts!
#define COZ_MEASURE 400
/* Includes */
#include <cstring>
2021-03-11 21:22:37 +01:00
#include <cstdint>
2021-03-11 21:38:06 +01:00
#include <cstdio>
#include <cstdlib> // std::rand | rand
#include <vector>
#include <chrono>
#include <algorithm> // std::sort
2021-03-11 21:22:37 +01:00
#include "magyarsort.h"
#ifdef SKA_SORT
#include "ska_sort.hpp"
#endif // SKA_SORT
2021-12-14 17:29:33 +01:00
#ifdef COZ_MEASURE
#include "coz.h"
#endif // COZ_MEASURE
/* Input generation and prerequisites */
#ifdef CREEL
#define MAGYAR_SORT_NIBBLE
#define PRINT_OUTPUT
static inline std::vector<uint32_t> GenerateInput() {
static constexpr uint32_t CreelHex[16] = {
// Homage to https://www.youtube.com/watch?v=ujb2CIWE8zY haha
// When doing nibbles these are visible all throughout all the
// steps and these will be easily readable in debugger in hex!
0x277,
0x806,
0x681,
0x462,
0x787,
0x163,
0x284,
0x166,
0x905,
0x518,
0x263,
0x395,
0x988,
0x307,
0x779,
0x721
};
std::vector<uint32_t> ret;
ret.resize(16);
memcpy(&ret[0], CreelHex, sizeof(CreelHex));
return ret;
}
#else
// Randomized values, no overrides
static inline std::vector<uint32_t> GenerateInput() {
std::vector<uint32_t> ret;
ret.resize(SORT_WIDTH);
for(size_t ek = 0; ek < SORT_WIDTH; ++ek) {
ret[ek] = (uint32_t)std::rand();
}
return ret;
}
#endif
/* Test entry point */
2021-03-11 21:22:37 +01:00
int main() {
/* Input */
std::vector<uint32_t> in1 = GenerateInput();;
std::vector<uint32_t> in2 = in1; // copy
#ifndef MEASURE_ONLY
auto stdBegin = std::chrono::high_resolution_clock::now();
#ifndef SKA_SORT
/* std::sort */
std::sort(std::begin(in2), std::end(in2));
#else // SKA_SORT
/* Ska-sort */
//ska_sort(std::begin(in2), std::end(in2));
std::vector<uint32_t> buffer(in2.size());
if (ska_sort_copy(std::begin(in2), std::end(in2), std::begin(buffer))) in2.swap(buffer);
#endif // SKA_SORT
auto stdEnd = std::chrono::high_resolution_clock::now();
#ifdef PRINT_OUTPUT
printf("std: ");
MagyarSort::debugArr(&in2[0], in2.size());
#endif // PRINT_OUTPUT
#endif // !MEASURE_ONLY
2021-12-13 02:18:08 +01:00
uint32_t *arr1 = &(in1[0]);
#ifdef PRINT_OUTPUT
printf("Inp: ");
MagyarSort::debugArr(arr1, in1.size());
#endif // PRINT_OUTPUT
/* Our sort */
auto ourBegin = std::chrono::high_resolution_clock::now();
MagyarSort::sort(arr1, in1.size());
auto ourEnd = std::chrono::high_resolution_clock::now();
2021-12-14 17:29:33 +01:00
#ifdef COZ_MEASURE
std::vector<uint32_t> in_coz_common = GenerateInput();;
for(int i = 0; i < COZ_MEASURE; ++i) {
std::vector<uint32_t> in_coz = in_coz_common; // cpy
uint32_t *arr_coz = &(in_coz[0]);
MagyarSort::sort(arr1, in1.size());
COZ_PROGRESS_NAMED("magyarsort_progress");
if((i % 128) == 0) { printf("%d / %d\n", i, COZ_MEASURE); }
}
#endif // COZ_MEASURE
2021-12-13 02:18:08 +01:00
#ifdef PRINT_OUTPUT
printf("Our: ");
MagyarSort::debugArr(arr1, in1.size());
#endif // PRINT_OUTPUT
/* Check against std - the real test */
2021-03-11 21:22:37 +01:00
bool good = true;
#ifndef MEASURE_ONLY
for(size_t i = 0; good && (i < in1.size()); ++i) {
good &= (in1[i] == in2[i]);
}
#endif // !MEASURE_ONLY
2021-03-11 21:22:37 +01:00
printf("Results:\n\n");
printf("- Sorted %zu elements", in1.size());
#ifndef MEASURE_ONLY
2021-12-14 17:29:33 +01:00
if(good) printf("- Same result as known sort result!\n");
else printf("- Differs from known sort result! Error!\n");
printf("\n");
auto stdElapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(stdEnd - stdBegin);
#endif // !MEASURE_ONLY
auto ourElapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(ourEnd - ourBegin);
#ifndef MEASURE_ONLY
2021-12-14 17:29:33 +01:00
#ifdef SKA_SORT
printf("Time (ska sort): %.3f ms.\n", stdElapsed.count() * 1e-6);
#else
printf("Time (std sort): %.3f ms.\n", stdElapsed.count() * 1e-6);
2021-12-14 17:29:33 +01:00
#endif
#endif // !MEASURE_ONLY
printf("Time (our sort): %.3f ms.\n", ourElapsed.count() * 1e-6);
2021-03-11 21:22:37 +01:00
return 0;
}