Skip to content

Commit

Permalink
adding a std::execution concurrency test to the playground
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Nov 22, 2024
1 parent 8ce093b commit ead0d37
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions playground/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
50 changes: 50 additions & 0 deletions playground/concurrency.cpp
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);
}

0 comments on commit ead0d37

Please sign in to comment.