This is an example program to compare the running speed and code complexity difference between C++ standard ranges operations and cpark operations with different execution cores.
#include <iostream>
#include <iomanip>
#include <ranges>
#include <chrono>
#include <thread>
#include "generator_rdd.h"
#include "transformed_rdd.h"
#include "filter_rdd.h"
#include "reduce.h"
inline std::chrono::time_point<std::chrono::high_resolution_clock> getCurrentTime() {
return std::chrono::high_resolution_clock::now();
}
inline long getTimeDifference(
std::chrono::time_point<std::chrono::high_resolution_clock> t0,
std::chrono::time_point<std::chrono::high_resolution_clock> t1) {
return std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count();
}
int main(int argc, char** argv) {
if (argc != 2) {
std::cerr << "Wrong command line arguments." << std::endl;
std::cerr << "Usage: " << argv[0] << " <N>" << std::endl;
exit(1);
}
int N = atoi(argv[1]);
auto std_begin_ts = getCurrentTime();
auto cpp_std_view =
std::views::iota(1, N + 1) |
std::views::transform([](auto x) { return x * x; }) |
std::views::transform([](auto x) {
int res = 0;
for (int i = 1; i <= x; i++) res += x;
return res;
}) |
std::views::filter([](auto x) { return x % 5 == 0; }) |
std::views::transform([](auto x) { return x + 2; }) |
std::views::filter([](auto x) { return x % 3 == 0; });
auto cpp_result = std::reduce(cpp_std_view.begin(), cpp_std_view.end(), 0, [](auto x, auto y) { return x + y; });
auto std_end_ts = getCurrentTime();
const long cpp_time = getTimeDifference(std_begin_ts, std_end_ts);
std::cout << cpp_result << std::endl;
std::cerr << "C++ standard way uses " << cpp_time << " ms\n";
const unsigned int hardware_concurrency = std::thread::hardware_concurrency();
for (int cores = 1; cores < 2 * hardware_concurrency; cores += 2) {
auto cpark_begin_ts = getCurrentTime();
auto cpark_result =
int res = 0;
for (int i = 1; i <= x; i++) res += x;
return res;
}) |
auto cpark_end_ts = getCurrentTime();
long temp_time = getTimeDifference(cpark_begin_ts, cpark_end_ts);
std::cout << cpark_result << std::endl;
std::cerr << "CPARK (" << cores <<" cores) uses " << temp_time << " ms\t";
std::cerr << std::fixed << std::setprecision(4) << "[" << (double)temp_time / cpp_time << "x]\n";
if (cores == hardware_concurrency || cores == hardware_concurrency - 1)
std::cerr << "===== Hardware concurrency : " << hardware_concurrency << " =====\n";
}
return 0;
}
Config & setParallelTaskNum(size_t num=0) noexcept
Definition cpark.h:82
Definition filter_rdd.h:161
Definition generator_rdd.h:27