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 std::mutex lock_console;
|
|
|
|
|
|
2017-09-24 14:01:30 +08:00
|
|
|
|
template <typename T>
|
2019-10-09 10:08:31 +08:00
|
|
|
|
void dump(size_t idx, std::string name, T start, T end, intptr_t count)
|
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 << " ";
|
2019-10-09 10:08:31 +08:00
|
|
|
|
std::cout << count << " " << ns << " ns ";
|
|
|
|
|
std::cout << (ns / count) << " ns/op" << " ";
|
|
|
|
|
std::cout << (count * 100ll * 1000ll / ns) << "w/cps" << std::endl;
|
2018-09-18 13:43:50 +08:00
|
|
|
|
|
|
|
|
|
lock_console.unlock();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 10:08:31 +08:00
|
|
|
|
static const intptr_t N = 3000000;
|
|
|
|
|
//static const int N = 10;
|
|
|
|
|
|
2020-02-08 21:05:44 +08:00
|
|
|
|
auto yield_switch(intptr_t coro) -> std::experimental::generator<intptr_t>
|
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);
|
2020-02-08 21:05:44 +08:00
|
|
|
|
go [=] ()->std::experimental::generator<intptr_t>
|
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;
|
2020-02-16 11:33:19 +08:00
|
|
|
|
co_return N / coro;
|
2017-09-24 14:01:30 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
2019-10-09 10:08:31 +08:00
|
|
|
|
auto middle = std::chrono::steady_clock::now();
|
|
|
|
|
dump(idx, "BenchmarkCreate_" + std::to_string(coro), start, middle, 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();
|
2019-10-09 10:08:31 +08:00
|
|
|
|
dump(idx, "BenchmarkSwitch_" + std::to_string(coro), middle, end, N);
|
2017-09-24 14:01:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void resumable_main_resumable()
|
|
|
|
|
{
|
2019-10-09 10:08:31 +08:00
|
|
|
|
resumable_switch(1, 0);
|
2018-09-18 13:43:50 +08:00
|
|
|
|
//resumable_switch(10, 0);
|
|
|
|
|
//resumable_switch(100, 0);
|
2019-10-09 10:08:31 +08:00
|
|
|
|
resumable_switch(1000, 0);
|
|
|
|
|
resumable_switch(30000, 0);
|
2018-08-08 21:01:25 +08:00
|
|
|
|
|
2019-10-09 10:08:31 +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;
|
|
|
|
|
}
|
2019-10-09 10:08:31 +08:00
|
|
|
|
*/
|
2017-09-24 14:01:30 +08:00
|
|
|
|
}
|