2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <conio.h>
|
|
|
|
|
|
|
|
|
|
#include "librf.h"
|
|
|
|
|
|
|
|
|
|
|
2018-09-18 13:43:50 +08:00
|
|
|
|
static const intptr_t N = 10000000;
|
2018-08-08 21:01:25 +08:00
|
|
|
|
//static const int N = 10;
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
2018-09-18 13:43:50 +08:00
|
|
|
|
static std::mutex lock_console;
|
|
|
|
|
|
2017-09-24 14:01:30 +08:00
|
|
|
|
template <typename T>
|
2018-09-18 13:43:50 +08:00
|
|
|
|
void dump(size_t idx, std::string name, T start, T end)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
2018-09-18 13:43:50 +08:00
|
|
|
|
lock_console.lock();
|
|
|
|
|
|
2017-09-24 14:01:30 +08:00
|
|
|
|
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
|
2018-09-18 13:43:50 +08:00
|
|
|
|
std::cout << idx << ":" << name << " ";
|
|
|
|
|
std::cout << N << " " << ns << " ns ";
|
|
|
|
|
std::cout << (ns / N) << " ns/op" << " ";
|
|
|
|
|
std::cout << (N * 100 * 1000 / ns) << "w/cps" << std::endl;
|
|
|
|
|
|
|
|
|
|
lock_console.unlock();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-18 13:43:50 +08:00
|
|
|
|
auto yield_switch(intptr_t coro)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
2018-09-18 13:43:50 +08:00
|
|
|
|
for (intptr_t i = 0; i < N / coro; ++i)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
co_yield i;
|
|
|
|
|
return N / coro;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-18 13:43:50 +08:00
|
|
|
|
void resumable_switch(intptr_t coro, size_t idx)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
2018-09-18 13:43:50 +08:00
|
|
|
|
resumef::local_scheduler ls;
|
|
|
|
|
|
2017-09-24 14:01:30 +08:00
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
|
|
2018-09-18 13:43:50 +08:00
|
|
|
|
for (intptr_t i = 0; i < coro; ++i)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
//go yield_switch(coro);
|
|
|
|
|
go [=]
|
|
|
|
|
{
|
2018-09-18 13:43:50 +08:00
|
|
|
|
for (intptr_t i = 0; i < N / coro; ++i)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
co_yield i;
|
|
|
|
|
return N / coro;
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-10-01 10:33:08 +08:00
|
|
|
|
resumef::this_scheduler()->run_until_notask();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
auto end = std::chrono::steady_clock::now();
|
2018-09-18 13:43:50 +08:00
|
|
|
|
dump(idx, "BenchmarkSwitch_" + std::to_string(coro), start, end);
|
2017-09-24 14:01:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void resumable_main_resumable()
|
|
|
|
|
{
|
2018-09-18 13:43:50 +08:00
|
|
|
|
//resumable_switch(1, 0);
|
|
|
|
|
//resumable_switch(10, 0);
|
|
|
|
|
//resumable_switch(100, 0);
|
|
|
|
|
//resumable_switch(10000, 0);
|
|
|
|
|
//resumable_switch(10000000, 0);
|
2018-08-08 21:01:25 +08:00
|
|
|
|
|
2018-09-18 13:43:50 +08:00
|
|
|
|
std::thread works[32];
|
|
|
|
|
for (size_t w = 1; w <= _countof(works); ++w)
|
|
|
|
|
{
|
|
|
|
|
for (size_t idx = 0; idx < w; ++idx)
|
|
|
|
|
works[idx] = std::thread(&resumable_switch, 1000, idx);
|
|
|
|
|
for (size_t idx = 0; idx < w; ++idx)
|
|
|
|
|
works[idx].join();
|
|
|
|
|
|
|
|
|
|
std::cout << std::endl << std::endl;
|
|
|
|
|
}
|
2017-09-24 14:01:30 +08:00
|
|
|
|
}
|