Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

taskflow 对比测例 #386

Open
ChunelFeng opened this issue Jun 10, 2024 · 1 comment
Open

taskflow 对比测例 #386

ChunelFeng opened this issue Jun 10, 2024 · 1 comment

Comments

@ChunelFeng
Copy link
Owner

ChunelFeng commented Jun 10, 2024

#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;
}
@ChunelFeng
Copy link
Owner Author

void demo4() {
    tf::Taskflow taskflow;
    g_test_cnt = 0;

    auto [A1,A2,A3,A4] = taskflow.emplace(
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); });
    auto [B1,B2, B3,B4] = taskflow.emplace(
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); });
    auto [C1,C2,C3,C4] = taskflow.emplace(
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); });
    auto [D1,D2,D3,D4] = taskflow.emplace(
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); },
            []() {
                g_test_cnt++;
                return CStatus(); });

    B1.succeed(A1, A2, A3, A4);
    B2.succeed(A1, A2, A3, A4);
    B3.succeed(A1, A2, A3, A4);
    B4.succeed(A1, A2, A3, A4);

    C1.succeed(B1, B2, B3, B4);
    C2.succeed(B1, B2, B3, B4);
    C3.succeed(B1, B2, B3, B4);
    C4.succeed(B1, B2, B3, B4);

    D1.succeed(C1, C2, C3, C4);
    D2.succeed(C1, C2, C3, C4);
    D3.succeed(C1, C2, C3, C4);
    D4.succeed(C1, C2, C3, C4);

    tf::Executor executor(4);
    auto start_ts_ = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < 100000; 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());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant