Skip to content

使用算法count_if()的并行版本来统计vector中偶数的数量 永远都不值得,即使有1,000,000,000个元素 #39

Open
@xunboo

Description

@xunboo

似乎这个理论有问题,实际测试:

auto eval = [](auto fun)
    {
        const auto t1 = std::chrono::high_resolution_clock::now();
        const auto [name, result] = fun();
        const auto t2 = std::chrono::high_resolution_clock::now();
        const std::chrono::duration<double, std::milli> ms = t2 - t1;
        std::cout << std::setw(28) << std::left << name << "sum: "
            << result << "\t time: " << ms.count() << " ms\n";
    };
{

    eval([&v] { return std::pair{ "std::count_if (seq, long)",
        std::count_if(SEQ
        v.begin(), v.end(),
        [](int elem) {
            return elem % 2 == 0;
        }) };  });
    eval([&v] { return std::pair{ "std::count_if (par, long)",
        std::count_if(PAR
        v.begin(), v.end(),
        [](int elem) {
            return elem % 2 == 0;
        }) };  });
        
        
   VS2022 C++17  release编译运行结果     

std::count_if (seq, long) sum: 100,000,007 time: 51.5 ms
std::count_if (par, long) sum: 100,000,007 time: 13.3 ms

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions