Skip to content

Instantly share code, notes, and snippets.

@yayj
Last active March 7, 2016 05:29
Show Gist options
  • Save yayj/8a2c5b5d1ffe152b9086 to your computer and use it in GitHub Desktop.
Save yayj/8a2c5b5d1ffe152b9086 to your computer and use it in GitHub Desktop.
Comparing io_service::post's performance with GCD's
#include <chrono>
#include <iostream>
#include <thread>
#include <dispatch/dispatch.h>
#include <boost/asio.hpp>
using namespace std::chrono_literals;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::chrono::system_clock;
using std::chrono::time_point;
namespace asio = boost::asio;
static auto const amount = 100000000L;
class TimePrinter {
public:
TimePrinter(std::string const& header) : header_(header), start_(system_clock::now()) {}
~TimePrinter()
{
auto delta = system_clock::now() - start_;
std::cout << header_ << ": " << duration_cast<milliseconds>(delta).count() << "ms.\n";
}
private:
std::string header_;
time_point<system_clock> start_;
};
static asio::io_service io;
void asio_testing()
{
TimePrinter printer{"ASIO"};
std::thread t{[] {
asio::io_service::work{io};
for (auto i = 0l; i < amount; ++i) {
io.post([] {});
}
}};
io.run();
t.join();
}
void dispatch_testing()
{
TimePrinter printer{"GCD"};
auto sema = dispatch_semaphore_create(0);
auto queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
for (auto i = 0l; i < amount; ++i) {
dispatch_async(queue, ^{});
}
dispatch_async(queue, ^{ dispatch_semaphore_signal(sema); });
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
int main()
{
asio_testing();
dispatch_testing();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment