-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding a std::execution concurrency test to the playground
- Loading branch information
1 parent
8ce093b
commit ead0d37
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag | ||
|
||
#include <algorithm> | ||
#include <chrono> | ||
#include <cstdint> | ||
#include <iostream> | ||
#include <random> | ||
#include <vector> | ||
|
||
#define PARALLEL | ||
#ifdef PARALLEL | ||
#include <execution> | ||
namespace execution = std::execution; | ||
#else | ||
enum class execution { seq, unseq, par_unseq, par }; | ||
#endif | ||
|
||
void measure([[maybe_unused]] auto policy, std::vector<std::uint64_t> v) | ||
{ | ||
const auto start = std::chrono::steady_clock::now(); | ||
#ifdef PARALLEL | ||
std::sort(policy, v.begin(), v.end()); | ||
#else | ||
std::sort(v.begin(), v.end()); | ||
#endif | ||
const auto finish = std::chrono::steady_clock::now(); | ||
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(finish - start) | ||
<< '\n'; | ||
}; | ||
|
||
int main() | ||
{ | ||
std::vector<std::uint64_t> v(1'000'000); | ||
std::mt19937 gen {std::random_device{}()}; | ||
std::ranges::generate(v, gen); | ||
/* | ||
1M random uint64_t's | ||
83ms | ||
74ms | ||
12ms | ||
12ms | ||
on an 8 core machine | ||
*/ | ||
measure(execution::seq, v); | ||
measure(execution::unseq, v); | ||
measure(execution::par_unseq, v); | ||
measure(execution::par, v); | ||
} |