yyzybb537 / libgo

Go-style concurrency in C++11
MIT License
3.21k stars 757 forks source link

它的执行任务速度似乎没有线程池快,协程任务调度耗费时间? #230

Closed nqf closed 4 years ago

nqf commented 4 years ago

线程池中采用一个线程

 int NUM = 10000;     
   {
       ThreadPool pool(1);

        auto start = getTimeStamp<std::chrono::milliseconds>();
        std::vector<std::future<void>> results;

        for (int i = 1; i <= NUM; ++i)
        {
            results.emplace_back(
                pool.enqueue([i, &start, &NUM] {
                    if (i == NUM)
                    {
                        std::cout << "thread_pool time:" << getTimeStamp<std::chrono::milliseconds>() - start << std::endl;
                    }
                }));
        }
        for (auto &ret : results)
            ret.wait();
        std::cout << "thread_pool time:" << getTimeStamp<std::chrono::milliseconds>() - start << std::endl;
    }
    std::cout << "开始libgo" << std::endl;
    {
        auto start = getTimeStamp<std::chrono::milliseconds>();
        for (int i = 1; i <= NUM; i++)
        {
            go[i, &start, &NUM]
            {
                if (i == NUM)
                {
                    std::cout << "libgo time:" << getTimeStamp<std::chrono::milliseconds>() - start << std::endl;
                }
            };
        }
    }

thread_pool time:23
thread_pool time:23
开始libgo
libgo time:74