diff --git a/playground/CMakeLists.txt b/playground/CMakeLists.txt index cde32018..506f5ca4 100644 --- a/playground/CMakeLists.txt +++ b/playground/CMakeLists.txt @@ -10,6 +10,7 @@ set(REAL_SRCS efunc_posits.cpp type_test.cpp float_to_decimal_string.cpp lazy_evaluation.cpp + concurrency.cpp expression_templates.cpp ) diff --git a/playground/concurrency.cpp b/playground/concurrency.cpp new file mode 100644 index 00000000..5f60e1f2 --- /dev/null +++ b/playground/concurrency.cpp @@ -0,0 +1,50 @@ +// https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag + +#include +#include +#include +#include +#include +#include + +#define PARALLEL +#ifdef PARALLEL +#include + namespace execution = std::execution; +#else + enum class execution { seq, unseq, par_unseq, par }; +#endif + +void measure([[maybe_unused]] auto policy, std::vector 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(finish - start) + << '\n'; +}; + +int main() +{ + std::vector 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); +} \ No newline at end of file