CPARK 1.0
A light-weighted, distributed computing framework for C++ that offers a fast and general-purpose large data processing solution.
Loading...
Searching...
No Matches
speed_check.cpp

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]);
/*
* Using the ranges and views in standard C++,
* generate values from 1 to N,
* compute its square value,
* sum up from 1 to this value,
* filter all numbers that can be divided by 5,
* add 2 to each of them,
* filter all numbers that can be divided by 3,
* compute reduce on this sequence.
*/
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";
/*
* Using our CPARK method to repeat everything again.
*/
const unsigned int hardware_concurrency = std::thread::hardware_concurrency();
for (int cores = 1; cores < 2 * hardware_concurrency; cores += 2) {
cpark::Config default_config;
default_config.setParallelTaskNum(cores);
cpark::ExecutionContext default_context{default_config};
auto cpark_begin_ts = getCurrentTime();
auto cpark_result =
cpark::GeneratorRdd(1, N + 1, [&](auto i) -> auto { return i; }, &default_context) |
cpark::Transform([](auto x) { return x * x; }) |
cpark::Transform([](auto x) {
int res = 0;
for (int i = 1; i <= x; i++) res += x;
return res;
}) |
cpark::Filter([](auto x) { return x % 5 == 0; }) |
cpark::Transform([](auto x) { return x + 2; }) |
cpark::Filter([](auto x) { return x % 3 == 0; }) |
cpark::Reduce([](auto x, auto y) { return x + y; });
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;
}
Definition cpark.h:35
Config & setParallelTaskNum(size_t num=0) noexcept
Definition cpark.h:82
Definition cpark.h:148
Definition filter_rdd.h:161
Definition generator_rdd.h:27
Definition reduce.h:24
Definition transformed_rdd.h:71