Skip to content

Instantly share code, notes, and snippets.

@BillyONeal
Created March 26, 2021 04:05
Show Gist options
  • Save BillyONeal/e6b1e9640e5730b2da212bf6aba7966b to your computer and use it in GitHub Desktop.
Save BillyONeal/e6b1e9640e5730b2da212bf6aba7966b to your computer and use it in GitHub Desktop.
future benchmark
#include <stdint.h>
#include <future>
#include <random>
#include <vector>
#include <benchmark/benchmark.h>
static uint_fast32_t long_running_task() {
std::mt19937 gen{std::random_device{}()};
gen.discard(1000);
return gen();
}
static void bench_async(benchmark::State& state) {
using namespace std;
for (auto _ : state) {
(void)_;
vector<future<uint_fast32_t>> futures(1000000);
for (auto& f : futures) {
f = async([] { return long_running_task(); });
}
for (auto& f : futures) {
f.wait();
}
}
}
BENCHMARK(bench_async);
BENCHMARK_MAIN();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment