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_parallel.cpp | |
| download | taming-cpp-2f19e94b0ce78d6ccc9eedbf04b8a3460fd05565.tar.gz taming-cpp-2f19e94b0ce78d6ccc9eedbf04b8a3460fd05565.zip | |
Sort benchmarks
Diffstat (limited to '')
| -rw-r--r-- | sort-benchmark/pairs/sort_parallel.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/sort-benchmark/pairs/sort_parallel.cpp b/sort-benchmark/pairs/sort_parallel.cpp new file mode 100644 index 0000000..ef9ee05 --- /dev/null +++ b/sort-benchmark/pairs/sort_parallel.cpp | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | /* Requires Thread Building Blocks (libtbb-dev on Debian) */ | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstdlib> | ||
| 5 | #include <ctime> | ||
| 6 | #include <execution> | ||
| 7 | #include <iostream> | ||
| 8 | |||
| 9 | typedef struct { int a, b; } pair_t; | ||
| 10 | pair_t a[ARRAYSIZE]; | ||
| 11 | |||
| 12 | int main(void) { | ||
| 13 | srand(time(NULL)); | ||
| 14 | for (auto &x : a) { | ||
| 15 | x.a = rand() % 1000000; | ||
| 16 | x.b = rand() % 1000000; | ||
| 17 | } | ||
| 18 | |||
| 19 | struct timespec begin; | ||
| 20 | clock_gettime(CLOCK_MONOTONIC, &begin); | ||
| 21 | |||
| 22 | std::sort(std::execution::par, a, a+ARRAYSIZE, | ||
| 23 | [](const pair_t &x, const pair_t &y) { | ||
| 24 | int d = x.a - y.a; | ||
| 25 | return d < 0 || (d == 0 && x.b > y.b); | ||
| 26 | }); | ||
| 27 | |||
| 28 | struct timespec end; | ||
| 29 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 30 | double time = end.tv_sec - begin.tv_sec + | ||
| 31 | (end.tv_nsec - begin.tv_nsec) / 1000000000.0; | ||
| 32 | |||
| 33 | std::cout << "(" << a[ARRAYSIZE/2].a << ") C++ time for " << ARRAYSIZE | ||
| 34 | << " pairs (parallel): " << time << "s\n"; | ||
| 35 | } | ||
