123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- // WETSort.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
-
- #include "pch.h"
- #include <random>
- #include <chrono>
- #include <ratio>
- #include <algorithm>
- #include <vector>
-
- #pragma pack(push, 1)
- template <size_t s>
- 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 <typename ElementType>
- double Sort0(const ElementType* data, size_t numItems)
- {
- std::vector<ElementType> 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<double, std::ratio<1, 1>>;
-
- auto start = clock.now();
-
- std::sort(items.begin(), items.end(), std::less<ElementType>());
-
- auto end = clock.now();
- return std::chrono::duration_cast<sec>(end - start).count();
- }
-
- void RunTest(size_t size)
- {
- std::random_device rdev;
- std::default_random_engine re(rdev());
- std::uniform_int_distribution<unsigned short> 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<Element<4>*>(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);
- }
-
|