Re: [asio-users] io_service post task so slow!
Brought to you by:
chris_kohlhoff
From: Gruenke, M. <mgruenke@Tycoint.com> - 2012-06-28 15:58:48
|
Your tests aren't measuring the same thing. Your ASIO test has a lot of boost::bind and boost::function overhead not present in your mutex queue test. For the test to be meaningful, the only difference should be ASIO strand vs. your mutex queue. Try changing your mutex queue to contain function objects. Then, enqueue boost::bind(&some_work, i) and have deque_test::work() call each entry before dequeuing it. BTW, I'd also use a condition variable, instead of sleep. I think the sleep is allowing the worker thread to get well ahead of the consumer, resulting in many fewer context switches than ASIO is experiencing. Matt From: he keding Sent: Wednesday, June 27, 2012 11:57 PM To: asi...@li... Subject: [asio-users] io_service post task so slow! hi all, I use io_service as my task queue, for example io_service ios; ... ios.post(...) but, i do a test recently, i find ios is much slower to my simple mutext deque, the code below #include <deque> #include <boost/asio.hpp> #include <boost/thread.hpp> #include <boost/timer/timer.hpp> const int TEST_COUNT = 2000000; void some_work(int w) { w += 2; } void test_io_service() { boost::asio::io_service ios; std::unique_ptr<boost::asio::io_service::work> w(new boost::asio::io_service::work(ios)); boost::thread t(boost::bind(&boost::asio::io_service::run, &ios)); printf("test_io_service\n"); boost::timer::auto_cpu_timer auto_timer; for (int i = 0; i < TEST_COUNT; ++i) { ios.post(boost::bind(&some_work, i)); } w.reset(); t.join(); } class deque_test { public: deque_test() { } void run_test() { boost::thread t(boost::bind(&deque_test::work, this)); printf("test_deque\n"); boost::timer::auto_cpu_timer auto_timer; for (int i = 0; i < TEST_COUNT; ++i) { boost::lock_guard<boost::mutex> g(m_); q_.push_back(i); } t.join(); } void work() { while (true) { boost::lock_guard<boost::mutex> g(m_); if (q_.empty()) { boost::this_thread::sleep(boost::posix_time::millisec(1)); continue; } int i = q_.front(); some_work(i); q_.pop_front(); if (i == TEST_COUNT - 1) { break; } } } private: boost::mutex m_; std::deque<int> q_; }; int main(int argc, char** argv) { test_io_service(); deque_test td; td.run_test(); /* get this result test_io_service 2.921010s wall, 1.484375s user + 3.046875s system = 4.531250s CPU (155.1%) test_deque 0.343004s wall, 0.187500s user + 0.031250s system = 0.218750s CPU (63.8%) */ getchar(); } ---------------------------- ps, I do another test, iocp vs mutext deque, i find iocp is similar to asio, so i think iocp queue is slower than deque. is replace iocp by deque as task queue simple? |