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_parallel.cpp | |
| download | taming-cpp-2f19e94b0ce78d6ccc9eedbf04b8a3460fd05565.tar.gz taming-cpp-2f19e94b0ce78d6ccc9eedbf04b8a3460fd05565.zip | |
Sort benchmarks
Diffstat (limited to 'sort-benchmark/integers/sort_parallel.cpp')
| -rw-r--r-- | sort-benchmark/integers/sort_parallel.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/sort-benchmark/integers/sort_parallel.cpp b/sort-benchmark/integers/sort_parallel.cpp new file mode 100644 index 0000000..5de4158 --- /dev/null +++ b/sort-benchmark/integers/sort_parallel.cpp | |||
| @@ -0,0 +1,28 @@ | |||
| 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 | int a[ARRAYSIZE]; | ||
| 10 | |||
| 11 | int main(void) { | ||
| 12 | srand(time(NULL)); | ||
| 13 | for (auto &x : a) x = rand() % 1000000000; | ||
| 14 | |||
| 15 | struct timespec begin; | ||
| 16 | clock_gettime(CLOCK_MONOTONIC, &begin); | ||
| 17 | |||
| 18 | std::sort(std::execution::par, a, a+ARRAYSIZE, | ||
| 19 | [](const int &x, const int &y) { return x < y; }); | ||
| 20 | |||
| 21 | struct timespec end; | ||
| 22 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 23 | double time = end.tv_sec - begin.tv_sec + | ||
| 24 | (end.tv_nsec - begin.tv_nsec) / 1000000000.0; | ||
| 25 | |||
| 26 | std::cout << "(" << a[ARRAYSIZE/2] << ") C++ time for " << ARRAYSIZE | ||
| 27 | << " numbers (parallel): " << time << "s\n"; | ||
| 28 | } | ||
