diff options
| author | Tyge Løvset <[email protected]> | 2023-02-12 22:47:55 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-02-12 23:20:18 +0100 |
| commit | 7dc6fddc079f4f572c8fb7c0ffd5a27e03291a2d (patch) | |
| tree | 681d1894d917bc2fe244375298ea40f736c18e18 /misc/benchmarks/various/csort_bench.c | |
| parent | 9904a7ea36f9e4f45d7e41e409ed23ad22821e8a (diff) | |
| download | STC-modified-7dc6fddc079f4f572c8fb7c0ffd5a27e03291a2d.tar.gz STC-modified-7dc6fddc079f4f572c8fb7c0ffd5a27e03291a2d.zip | |
Fairly large update before release 4.1, cleaning up docs and some minor additions.
Diffstat (limited to 'misc/benchmarks/various/csort_bench.c')
| -rw-r--r-- | misc/benchmarks/various/csort_bench.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/misc/benchmarks/various/csort_bench.c b/misc/benchmarks/various/csort_bench.c new file mode 100644 index 00000000..97885eb8 --- /dev/null +++ b/misc/benchmarks/various/csort_bench.c @@ -0,0 +1,61 @@ +// Generic Quicksort in C, performs as fast as c++ std::sort(). +#include <stdio.h> +#include <time.h> +#include <stdlib.h> +#ifdef __cplusplus + #include <algorithm> +#endif +#define i_val int +#include <stc/algo/csort.h> + +#define ROTL(d,bits) ((d<<(bits)) | (d>>(8*sizeof(d)-(bits)))) +uint64_t random(uint64_t s[3]) { + uint64_t xp = s[0], yp = s[1], zp = s[2]; + s[0] = 15241094284759029579u * zp; + s[1] = yp - xp; s[1] = ROTL(s[1], 12); + s[2] = zp - yp; s[2] = ROTL(s[2], 44); + return xp; +} + +void testsort(int *a, int size, const char *desc) { + clock_t t = clock(); +#ifdef __cplusplus + { printf("std::sort: "); std::sort(a, a + size); } +#else + { printf("stc_sort: "); csort_int(a, size); } +#endif + t = clock() - t; + + printf("time: %.1fms, n: %d, %s\n", + (double)t*1000.0/CLOCKS_PER_SEC, size, desc); +} + + +int main(int argc, char *argv[]) { + size_t i, size = argc > 1 ? strtoull(argv[1], NULL, 0) : 10000000; + uint64_t s[3] = {123456789, 3456789123, 789123456}; + + int32_t *a = (int32_t*)malloc(sizeof(*a) * size); + if (!a) return -1; + + for (i = 0; i < size; i++) + a[i] = random(s) & (1U << 30) - 1; + testsort(a, size, "random"); + for (i = 0; i < 20; i++) + printf(" %d", (int)a[i]); + puts(""); + for (i = 0; i < size; i++) + a[i] = i; + testsort(a, size, "sorted"); + for (i = 0; i < size; i++) + a[i] = size - i; + testsort(a, size, "reverse sorted"); + for (i = 0; i < size; i++) + a[i] = 126735; + testsort(a, size, "constant"); + for (i = 0; i < size; i++) + a[i] = i + 1; + a[size - 1] = 0; + testsort(a, size, "rotated"); + free(a); +} |
