diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-04-29 21:59:51 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-04-29 21:59:51 +0200 |
| commit | 2f19e94b0ce78d6ccc9eedbf04b8a3460fd05565 (patch) | |
| tree | bc3a8cea30ecca96ed7ed779a1a4149ef9baa068 /sort-benchmark/pairs/sort.c | |
| download | taming-cpp-2f19e94b0ce78d6ccc9eedbf04b8a3460fd05565.tar.gz taming-cpp-2f19e94b0ce78d6ccc9eedbf04b8a3460fd05565.zip | |
Sort benchmarks
Diffstat (limited to '')
| -rw-r--r-- | sort-benchmark/pairs/sort.c | 38 | ||||
| -rw-r--r-- | sort-benchmark/pairs/sort.cpp | 32 |
2 files changed, 70 insertions, 0 deletions
diff --git a/sort-benchmark/pairs/sort.c b/sort-benchmark/pairs/sort.c new file mode 100644 index 0000000..826a684 --- /dev/null +++ b/sort-benchmark/pairs/sort.c | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | #define _POSIX_C_SOURCE 200809L /* Required to use clock_gettime */ | ||
| 2 | |||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <time.h> | ||
| 6 | |||
| 7 | typedef struct { int a, b; } pair_t; | ||
| 8 | pair_t a[ARRAYSIZE]; | ||
| 9 | |||
| 10 | int compar(const void *x, const void *y) { | ||
| 11 | pair_t *p = (pair_t *)x; | ||
| 12 | pair_t *q = (pair_t *)y; | ||
| 13 | int d = p->a - q->a; | ||
| 14 | return d > 0 ? 1 : (d < 0 ? -1 : (q->b - p->b)); | ||
| 15 | } | ||
| 16 | |||
| 17 | int main() { | ||
| 18 | srand(time(NULL)); | ||
| 19 | for (int i = 0; i < ARRAYSIZE; i++) { | ||
| 20 | a[i].a = rand() % 1000000; | ||
| 21 | a[i].b = rand() % 1000000; | ||
| 22 | } | ||
| 23 | |||
| 24 | struct timespec begin; | ||
| 25 | clock_gettime(CLOCK_MONOTONIC, &begin); | ||
| 26 | |||
| 27 | qsort(a, ARRAYSIZE, sizeof(pair_t), compar); | ||
| 28 | |||
| 29 | struct timespec end; | ||
| 30 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 31 | double time = end.tv_sec - begin.tv_sec + | ||
| 32 | (end.tv_nsec - begin.tv_nsec) / 1000000000.0; | ||
| 33 | |||
| 34 | printf("(%d) C time for %d pairs: %lfs\n", | ||
| 35 | a[ARRAYSIZE/2].a, ARRAYSIZE, time); | ||
| 36 | |||
| 37 | return 0; | ||
| 38 | } | ||
diff --git a/sort-benchmark/pairs/sort.cpp b/sort-benchmark/pairs/sort.cpp new file mode 100644 index 0000000..77ab5f0 --- /dev/null +++ b/sort-benchmark/pairs/sort.cpp | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <cstdlib> | ||
| 3 | #include <ctime> | ||
| 4 | #include <iostream> | ||
| 5 | |||
| 6 | typedef struct { int a, b; } pair_t; | ||
| 7 | pair_t a[ARRAYSIZE]; | ||
| 8 | |||
| 9 | int main(void) { | ||
| 10 | srand(time(NULL)); | ||
| 11 | for (auto &x : a) { | ||
| 12 | x.a = rand() % 1000000; | ||
| 13 | x.b = rand() % 1000000; | ||
| 14 | } | ||
| 15 | |||
| 16 | struct timespec begin; | ||
| 17 | clock_gettime(CLOCK_MONOTONIC, &begin); | ||
| 18 | |||
| 19 | std::sort(a, a+ARRAYSIZE, | ||
| 20 | [](const pair_t &x, const pair_t &y) { | ||
| 21 | int d = x.a - y.a; | ||
| 22 | return d < 0 || (d == 0 && x.b > y.b); | ||
| 23 | }); | ||
| 24 | |||
| 25 | struct timespec end; | ||
| 26 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 27 | double time = end.tv_sec - begin.tv_sec + | ||
| 28 | (end.tv_nsec - begin.tv_nsec) / 1000000000.0; | ||
| 29 | |||
| 30 | std::cout << "(" << a[ARRAYSIZE/2].a << ") C++ time for " << ARRAYSIZE | ||
| 31 | << " pairs: " << time << "s\n"; | ||
| 32 | } | ||
