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/integers/sort.c | |
| download | taming-cpp-2f19e94b0ce78d6ccc9eedbf04b8a3460fd05565.tar.gz taming-cpp-2f19e94b0ce78d6ccc9eedbf04b8a3460fd05565.zip | |
Sort benchmarks
Diffstat (limited to '')
| -rw-r--r-- | sort-benchmark/integers/sort.c | 29 | ||||
| -rw-r--r-- | sort-benchmark/integers/sort.cpp | 25 |
2 files changed, 54 insertions, 0 deletions
diff --git a/sort-benchmark/integers/sort.c b/sort-benchmark/integers/sort.c new file mode 100644 index 0000000..2a23d9d --- /dev/null +++ b/sort-benchmark/integers/sort.c | |||
| @@ -0,0 +1,29 @@ | |||
| 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 | int a[ARRAYSIZE]; | ||
| 8 | |||
| 9 | int compar(const void *x, const void *y) { return *(int *)x - *(int *)y; } | ||
| 10 | |||
| 11 | int main() { | ||
| 12 | srand(time(NULL)); | ||
| 13 | for (int i = 0; i < ARRAYSIZE; i++) a[i] = rand() % 1000000000; | ||
| 14 | |||
| 15 | struct timespec begin; | ||
| 16 | clock_gettime(CLOCK_MONOTONIC, &begin); | ||
| 17 | |||
| 18 | qsort(a, ARRAYSIZE, sizeof(int), compar); | ||
| 19 | |||
| 20 | struct timespec end; | ||
| 21 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 22 | double time = end.tv_sec - begin.tv_sec + | ||
| 23 | (end.tv_nsec - begin.tv_nsec) / 1000000000.0; | ||
| 24 | |||
| 25 | printf("(%d) C time for %d numbers: %lfs\n", | ||
| 26 | a[ARRAYSIZE/2], ARRAYSIZE, time); | ||
| 27 | |||
| 28 | return 0; | ||
| 29 | } | ||
diff --git a/sort-benchmark/integers/sort.cpp b/sort-benchmark/integers/sort.cpp new file mode 100644 index 0000000..3434d7f --- /dev/null +++ b/sort-benchmark/integers/sort.cpp | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <cstdlib> | ||
| 3 | #include <ctime> | ||
| 4 | #include <iostream> | ||
| 5 | |||
| 6 | int a[ARRAYSIZE]; | ||
| 7 | |||
| 8 | int main(void) { | ||
| 9 | srand(time(NULL)); | ||
| 10 | for (auto &x : a) x = rand() % 1000000000; | ||
| 11 | |||
| 12 | struct timespec begin; | ||
| 13 | clock_gettime(CLOCK_MONOTONIC, &begin); | ||
| 14 | |||
| 15 | std::sort(a, a+ARRAYSIZE, | ||
| 16 | [](const int &x, const int &y) { return x < y; }); | ||
| 17 | |||
| 18 | struct timespec end; | ||
| 19 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 20 | double time = end.tv_sec - begin.tv_sec + | ||
| 21 | (end.tv_nsec - begin.tv_nsec) / 1000000000.0; | ||
| 22 | |||
| 23 | std::cout << "(" << a[ARRAYSIZE/2] << ") C++ time for " << ARRAYSIZE | ||
| 24 | << " numbers: " << time << "s\n"; | ||
| 25 | } | ||
