-
Notifications
You must be signed in to change notification settings - Fork 362
Open
Description
#include <taskflow/taskflow.hpp> // the only include you need
class CStatus {
int code = 0;
};
std::atomic<unsigned int> g_test_cnt = {0};
void demo1() {
tf::Executor executor(8);
tf::Taskflow taskflow("simple");
// 并行的32路
g_test_cnt = 0;
for (int i = 0; i < 32; i++) {
auto x = taskflow.emplace([] {
g_test_cnt++;
return CStatus();
});
}
auto start_ts_ = std::chrono::high_resolution_clock::now();
for(int i = 0; i < 500000; i++) {
executor.run(taskflow).wait();
}
std::chrono::duration<double, std::milli> span = std::chrono::high_resolution_clock::now() - start_ts_;
printf("----> [taskflow] time cost is : [%0.2lf] ms \n", span.count());
}
void demo2() {
// 串行32个
tf::Executor executor(1);
tf::Taskflow taskflow;
const int length = 32;
std::vector<tf::Task> tasks(length);
for (auto i = 0; i < length; ++i) {
tasks[i] = taskflow.emplace([&] { g_test_cnt++; });
}
g_test_cnt = 0;
taskflow.linearize(tasks);
auto start_ts_ = std::chrono::high_resolution_clock::now();
for(int i = 0; i < 1000000; i++) {
executor.run(taskflow).wait();
}
std::chrono::duration<double, std::milli> span = std::chrono::high_resolution_clock::now() - start_ts_;
printf("----> [taskflow] time cost is : [%0.2lf] ms \n",
span.count());
}
void demo3() {
// 简单dag图
tf::Taskflow taskflow;
g_test_cnt = 0;
auto [A, B1, B2, C1, C2, D] = taskflow.emplace(
// []() { return std::this_thread::sleep_for(std::chrono::milliseconds(1)); },
[]() { g_test_cnt++; return CStatus(); },
[]() { g_test_cnt++; return CStatus(); },
[]() { g_test_cnt++; return CStatus(); },
[]() { g_test_cnt++; return CStatus(); },
[]() { g_test_cnt++; return CStatus(); },
[]() { g_test_cnt++; return CStatus(); }
);
A.precede(B1, C1);
B1.precede(B2);
C1.precede(C2);
D.succeed(B2, C2);
// execute the workflow
tf::Executor executor(2);
auto start_ts_ = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000000; i++) {
executor.run(taskflow).wait();
}
std::chrono::duration<double, std::milli> span = std::chrono::high_resolution_clock::now() - start_ts_;
printf("----> [taskflow] time cost is : [%0.2lf] ms \n",
span.count());
}
int main(){
for (int i = 0; i < 5; i++) {
//demo1();
demo2();
//demo3();
std::cout << g_test_cnt << std::endl;
}
return 0;
}
Metadata
Metadata
Assignees
Labels
No labels