BAN: Expose radix sort with user provided buffer
This can be nice if user has memory for a the temporary buffer and doesnt want the sorting to allocate or be able to fail. Also counts are now stack allocated, there isn't really any reason to allocate them on the heap as 256x 64 bit values only adds up to 2 KiB
This commit is contained in:
@@ -39,6 +39,32 @@ bool is_sorted(BAN::Vector<T>& vec)
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
||||
#define TEST_ALGORITHM_RADIX(ms) do { \
|
||||
uint64_t duration_us = 0; \
|
||||
printf("radix with preallocated buffer\n"); \
|
||||
for (size_t size = 100; duration_us < ms * 1000; size *= 10) { \
|
||||
BAN::Vector<unsigned> data(size, 0); \
|
||||
for (auto& val : data) \
|
||||
val = rand() % 100; \
|
||||
BAN::Vector<unsigned> temp(size); \
|
||||
uint64_t start_ns = CURRENT_NS(); \
|
||||
BAN::sort::radix_sort(data.begin(), data.end(), temp.span()); \
|
||||
uint64_t stop_ns = CURRENT_NS(); \
|
||||
if (!is_sorted(data)) { \
|
||||
printf(" \e[31mFAILED!\e[m\n"); \
|
||||
break; \
|
||||
} \
|
||||
duration_us = (stop_ns - start_ns) / 1'000; \
|
||||
printf(" %5d.%03d ms (%zu)\n", \
|
||||
(int)(duration_us / 1000), \
|
||||
(int)(duration_us % 1000), \
|
||||
size \
|
||||
); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
||||
#define TEST_ALGORITHM_QSORT(ms) do { \
|
||||
uint64_t duration_us = 0; \
|
||||
printf("qsort\n"); \
|
||||
@@ -72,5 +98,6 @@ int main()
|
||||
TEST_ALGORITHM(100, BAN::sort::intro_sort);
|
||||
TEST_ALGORITHM(1000, BAN::sort::sort);
|
||||
TEST_ALGORITHM(1000, BAN::sort::radix_sort);
|
||||
TEST_ALGORITHM_RADIX(1000);
|
||||
TEST_ALGORITHM_QSORT(100);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user