// WETSort.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include "pch.h" #include #include #include #include #include #pragma pack(push, 1) template struct Element { int8_t buffer; bool operator < (const Element& other) const { const int8_t* p0 = &buffer; const int8_t* p1 = &other.buffer; for (size_t i = 0; i < s; ++i) { int n = p0 - p1; if (n) return n > 0; p0++; p1++; } return false; } }; #pragma pack(pop) template double Sort0(const ElementType* data, size_t numItems) { std::vector items; items.reserve(numItems); for (size_t i = 0; i < numItems; ++i) { items.push_back(data[i]); } std::chrono::high_resolution_clock clock; using sec = std::chrono::duration>; auto start = clock.now(); std::sort(items.begin(), items.end(), std::less()); auto end = clock.now(); return std::chrono::duration_cast(end - start).count(); } void RunTest(size_t size) { std::random_device rdev; std::default_random_engine re(rdev()); std::uniform_int_distribution dist(0, 0xff); if (1) { size_t s = 64; while (s < size) s <<= 1; size = s; } int32_t* buffer = (int32_t*) malloc(size); int16_t tmp = 0; for (size_t i = 0, e = size / sizeof(int32_t); i < e; ++i) { int16_t x = dist(re); int16_t y = (i ^ (tmp << 4)) & 0xff; tmp = y; buffer[i] = (x << 16) | y; } size_t numItems = size / 4; double d1 = Sort0(reinterpret_cast*>(buffer), numItems); printf("std::sort items:%d elapsed:%f\n", numItems, d1); free(buffer); } int main() { printf("Sorting 1,000,000 bytes...\n"); RunTest(1000000); }