123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- // WETSort.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
-
-
- #include "../arkiny/dclb/Sort.h"
- #include "../ccm9026/JFSort/JFSort.h"
- #include "../djarksnd/sort.h"
- #include "../insun/Sort/Sort/SquickSort.h"
- #define partition es_partition
- #include "../prauser/ESSort.h"
- #undef partition
- #include "../witdrg/wc.h"
-
- #include <random>
- #include <chrono>
- #include <ratio>
- #include <algorithm>
- #include <vector>
- #include <functional>
-
-
- std::string sha1(const void* p, size_t s);
-
- const size_t testLength[] = {
- #ifdef _DEBUG
- 1ULL << 21,
- 1ULL << 24,
- 1ULL << 29,
- #else
- 1ULL << 31,
- 1ULL << 32,
- 1ULL << 33
- #endif
- };
-
- enum TestUnitId
- {
- stl = -1,
- arkiny = 0,
- ccm9026,
- djarksnd,
- insun,
- prauser,
- witdrg,
- MAX_UID
- };
-
- const char* uidString[MAX_UID] = {
- "arkiny",
- "ccm9026",
- "djarksnd",
- "insun",
- "prauser",
- "witdrg"
- };
-
- struct TestScore
- {
- bool error;
- double sum;
- int num;
- };
- TestScore testScore[MAX_UID] = { 0 };
-
- struct TestResult
- {
- double t;
- std::string hash;
- };
-
- enum GenerateDataTypes
- {
- GenUniform32,
- GenUniform16,
- GenUniform16Xor,
- GenUniform8,
- GenUniform8Xor,
- GenNormal64,
- GenNormal32,
- };
-
- #pragma pack(push, 1)
- template <size_t s>
- struct Element
- {
- int8_t buffer[s];
- bool operator < (const Element& other) const
- {
- for (size_t i = 0; i < s; ++i)
- {
- int n = int(buffer[i]) - int(other.buffer[i]);
- if (n)
- return n > 0;
- }
- return false;
- }
- };
- #pragma pack(pop)
-
- static std::vector<uint8_t> data0, data1;
-
- template <typename ElementType>
- TestResult TestUnit(int index)
- {
- std::function<void(ElementType*, size_t)> fn;
- switch (index)
- {
- case stl:
- fn = [](ElementType* items, size_t numItems)
- {
- std::sort(&items[0], &items[numItems], std::less<ElementType>());
- };
- break;
- case arkiny:
- fn = [](ElementType* items, size_t numItems)
- {
- dc::sort(&items[0], &items[numItems], std::less<ElementType>());
- };
- break;
- case ccm9026:
- fn = [](ElementType* items, size_t numItems)
- {
- JFFoundation::JFAlgorithm::_Sort(&items[0], &items[numItems], std::less<ElementType>());
- };
- break;
- case djarksnd:
- fn = [](ElementType* items, size_t numItems)
- {
- sort::sort(&items[0], &items[numItems], std::less<ElementType>());
- };
- break;
- case insun:
- fn = [](ElementType* items, size_t numItems)
- {
- // ERROR
- //SunSort::SSort(items, &items[0], &items[numItems - 1], std::less<ElementType>());
- };
- break;
- case prauser:
- fn = [](ElementType* items, size_t numItems)
- {
- // ERROR
- //std::less<ElementType> tmp;
- //ES::quickSort<ElementType*, std::less<ElementType>>(&items[0], &items[numItems], tmp);
- };
- break;
- case witdrg:
- fn = [](ElementType* items, size_t numItems)
- {
- // ERROR
- //wc::sort(&items[0], &items[numItems], std::less<ElementType>());
- };
- break;
- }
-
- if (fn)
- {
- std::chrono::high_resolution_clock clock;
- using sec = std::chrono::duration<double, std::ratio<1, 1>>;
-
- data1.resize(data0.size());
- memcpy(data1.data(), data0.data(), data0.size());
-
- size_t s = sizeof(ElementType);
- size_t numItems = data1.size() / s;
-
- auto start = clock.now();
- fn(reinterpret_cast<ElementType*>(data1.data()), numItems);
- auto end = clock.now();
-
- TestResult res;
- res.t = std::chrono::duration_cast<sec>(end - start).count();
- res.hash = sha1(data1.data(), data1.size()).c_str();
- return res;
- }
- throw std::runtime_error("unexpected error!");
- }
-
- template <size_t unitSize>
- void DoSortTest(void)
- {
- size_t numItems = data0.size() / unitSize;
- TestResult base = TestUnit<Element<unitSize>>(stl);
- printf("[%s] items: %zu / %zu, elapsed: %f, hash: %s\n", "STD", unitSize, numItems, base.t, base.hash.c_str());
- for (int i = 0; i < MAX_UID; ++i)
- {
- TestScore& score = testScore[i];
- if (!score.error)
- {
- //printf("[%s] sorting...\n", uidString[i]);
- TestResult r = TestUnit<Element<unitSize>>(i);
- if (base.hash == r.hash)
- {
- double s = base.t / r.t;
- score.sum += s;
- score.num++;
- printf("[%s] items: %zu, elapsed: %f, score:%d\n", uidString[i], numItems, r.t, int(s * 100));
- }
- else
- {
- printf("[%s] error!! [REJECTED]\n", uidString[i]);
- score.error = true;
- }
- }
- }
- }
-
- void RunTest(size_t size, GenerateDataTypes type)
- {
- std::random_device rdev;
- std::default_random_engine re(rdev());
-
- if (1)
- {
- size_t s = 64;
- while (s < size)
- s <<= 1;
- size = s;
- }
-
- // generate random number.
- data0.clear();
- data0.resize(size);
-
- switch (type)
- {
- case GenUniform32:
- if (1)
- {
- printf("Generating random data... (%zu bytes, type: Uniform32)\n", size);
- std::uniform_int_distribution<uint32_t> dist(0, 0xffffffff);
- for (size_t i = 0, e = size / sizeof(uint32_t); i < e; ++i)
- {
- reinterpret_cast<uint32_t*>(data0.data())[i] = dist(re);
- }
- }
- break;
- case GenUniform16:
- if (1)
- {
- printf("Generating random data... (%zu bytes, type: Uniform16)\n", size);
- std::uniform_int_distribution<uint16_t> dist(0, 0xffff);
- for (size_t i = 0, e = size / sizeof(uint32_t); i < e; ++i)
- {
- reinterpret_cast<uint32_t*>(data0.data())[i] = dist(re);
- }
- }
- break;
- case GenUniform16Xor:
- if (1)
- {
- printf("Generating random data... (%zu bytes, type: Uniform16Xor)\n", size);
- std::uniform_int_distribution<uint16_t> dist(0, 0xffff);
- uint32_t tmp = dist(re);
- for (size_t i = 0, e = size / sizeof(uint32_t); i < e; ++i)
- {
- uint32_t x = dist(re);
- uint32_t y = (i ^ (tmp << 3)) & 0xff;
- tmp = y;
- reinterpret_cast<uint32_t*>(data0.data())[i] = (x << 16) | y;
- }
- }
- break;
- case GenUniform8:
- if (1)
- {
- printf("Generating random data... (%zu bytes, type: Uniform8)\n", size);
- std::uniform_int_distribution<uint16_t> dist(0, 0xff);
- for (size_t i = 0, e = size / sizeof(uint32_t); i < e; ++i)
- {
- reinterpret_cast<uint32_t*>(data0.data())[i] = dist(re);
- }
- }
- break;
- case GenUniform8Xor:
- if (1)
- {
- printf("Generating random data... (%zu bytes, type: Uniform8Xor)\n", size);
- std::uniform_int_distribution<uint16_t> dist(0, 0xff);
- uint32_t tmp = dist(re);
- for (size_t i = 0, e = size / sizeof(uint32_t); i < e; ++i)
- {
- uint32_t val = dist(re);
- val = val ^ (tmp << 1);
- tmp = val;
- reinterpret_cast<uint32_t*>(data0.data())[i] = val;
- }
- }
- break;
- case GenNormal64:
- if (1)
- {
- printf("Generating random data... (%zu bytes, type: Normal64)\n", size);
- std::normal_distribution<double> dist;
- for (size_t i = 0, e = size / sizeof(double); i < e; ++i)
- {
- reinterpret_cast<double*>(data0.data())[i] = dist(re);
- }
- }
- break;
- case GenNormal32:
- if (1)
- {
- printf("Generating random data... (%zu bytes, type: Normal32)\n", size);
- std::normal_distribution<float> dist;
- for (size_t i = 0, e = size / sizeof(float); i < e; ++i)
- {
- reinterpret_cast<float*>(data0.data())[i] = dist(re);
- }
- }
- break;
- }
-
- // 1 byte
- DoSortTest<1>();
- // 2 byte
- DoSortTest<2>();
- // 3 bytes
- DoSortTest<3>();
- // 4 bytes
- DoSortTest<4>();
- // 5 bytes
- DoSortTest<5>();
- // 6
- DoSortTest<6>();
- // 7
- DoSortTest<7>();
- // 9 bytes
- DoSortTest<9>();
- // 10
- DoSortTest<10>();
- // 11
- DoSortTest<11>();
- // 12
- DoSortTest<12>();
- // 13
- DoSortTest<13>();
- // 15
- DoSortTest<15>();
- // 17 bytes
- DoSortTest<17>();
- //// 32 bytes
- //DoSortTest<32>();
- //// 33 bytes
- //DoSortTest<33>();
- }
-
- int main()
- {
- // Set .error to '0' to run the test!
- testScore[arkiny].error = 1;
- testScore[ccm9026].error = 1;
- testScore[djarksnd].error = 1;
- testScore[insun].error = 1;
- testScore[prauser].error = 1;
- testScore[witdrg].error = 1;
-
- auto PrintScore = [](int phase)
- {
- printf("------------ SCORE (%d/%d) --------------\n", phase, int(std::size(testLength)));
- for (int i = 0; i < MAX_UID; ++i)
- {
- const TestScore& score = testScore[i];
- if (score.error)
- {
- printf("UID(%s) rejected. (ERROR)\n", uidString[i]);
- }
- else
- {
- uint32_t s = (score.sum / double(score.num)) * 100.0;
- const char* t = "";
- if (s > 100)
- t = "(PERFECT)";
- else if (s > 90)
- t = "(GOOD)";
- else if (s < 60)
- t = "(POOR)";
- printf("UID(%s) score: %d. %s\n", uidString[i], s, t);
- }
- }
- printf("-----------------------------------------\n\n");
- };
- int index = 1;
- for (size_t n : testLength)
- {
- printf("========= Phase %d / %d (%zu) ==========\n", index, int(std::size(testLength)), n);
- for (auto t : {
- GenUniform32,
- GenUniform16,
- GenUniform16Xor,
- GenUniform8,
- GenUniform8Xor,
- GenNormal64,
- GenNormal32
- })
- {
- RunTest(n, t);
- }
- //RunTest(n, GenNormal64);
- PrintScore(index);
- index++;
- }
- }
|