129 lines
3.1 KiB
C++
129 lines
3.1 KiB
C++
/* 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 40000
|
|
// 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
|
|
|
|
/* Includes */
|
|
|
|
#include <cstring>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib> // std::rand | rand
|
|
#include <vector>
|
|
#include <chrono>
|
|
#include <algorithm> // std::sort
|
|
#include "magyarsort.h"
|
|
|
|
/* 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 */
|
|
|
|
int main() {
|
|
/* Input */
|
|
std::vector<uint32_t> in1 = GenerateInput();;
|
|
std::vector<uint32_t> in2 = in1; // copy
|
|
|
|
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();
|
|
|
|
#ifdef PRINT_OUTPUT
|
|
printf("Our: ");
|
|
MagyarSort::debugArr(arr1, in1.size());
|
|
#endif // PRINT_OUTPUT
|
|
|
|
/* std::sort */
|
|
auto stdBegin = std::chrono::high_resolution_clock::now();
|
|
std::sort(std::begin(in2), std::end(in2));
|
|
auto stdEnd = std::chrono::high_resolution_clock::now();
|
|
|
|
#ifdef PRINT_OUTPUT
|
|
printf("std: ");
|
|
MagyarSort::debugArr(&in2[0], in2.size());
|
|
#endif // PRINT_OUTPUT
|
|
|
|
/* Check against std - the real test */
|
|
|
|
bool good = true;
|
|
for(size_t i = 0; good && (i < in1.size()); ++i) {
|
|
good &= (in1[i] == in2[i]);
|
|
}
|
|
|
|
printf("Results:\n\n");
|
|
printf("- Sorted %zu elements", in1.size());
|
|
if(good) printf("- Same result as std::sort!\n");
|
|
else printf("- Differs from std::sort! Error!\n");
|
|
printf("\n");
|
|
auto stdElapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(stdEnd - stdBegin);
|
|
auto ourElapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(ourEnd - ourBegin);
|
|
printf("Time (std sort): %.3f ms.\n", stdElapsed.count() * 1e-6);
|
|
printf("Time (our sort): %.3f ms.\n", ourElapsed.count() * 1e-6);
|
|
|
|
return 0;
|
|
}
|