1
0
mirror of https://github.com/tearshark/librf.git synced 2024-10-01 15:57:07 +08:00
librf/benchmark/benchmark_channel_passing_next.cpp

82 lines
1.7 KiB
C++
Raw Normal View History

2019-04-25 09:16:06 +08:00

#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include <deque>
#include <mutex>
#include "librf.h"
2021-11-01 17:59:08 +08:00
using namespace librf;
2019-04-25 09:16:06 +08:00
using namespace std::chrono;
using namespace std::literals;
2020-03-09 17:59:07 +08:00
const static auto MaxNum = 100000;
const static auto LoopCount = 100;
2019-04-25 09:16:06 +08:00
using int_channel_ptr = std::shared_ptr<channel_t<intptr_t>>;
2020-03-09 17:59:07 +08:00
static future_t<> passing_next(channel_t<intptr_t> rd, channel_t<intptr_t> wr)
2019-04-25 09:16:06 +08:00
{
2020-03-09 17:59:07 +08:00
for (int i = 0; i < LoopCount; ++i)
2019-04-25 09:16:06 +08:00
{
2020-03-09 17:59:07 +08:00
intptr_t value = co_await rd;
co_await wr.write(value + 1);
2019-04-25 09:16:06 +08:00
}
}
2020-04-18 11:46:29 +08:00
static future_t<> passing_loop_all(channel_t<intptr_t> head, channel_t<intptr_t> tail)
{
for (int i = 0; i < LoopCount; ++i)
{
auto tstart = high_resolution_clock::now();
co_await(head << 0);
intptr_t value = co_await tail;
auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
std::cout << value << " cost time " << dt << "s" << std::endl;
}
}
2019-04-25 09:16:06 +08:00
void benchmark_main_channel_passing_next()
{
channel_t<intptr_t> head{1};
channel_t<intptr_t> in = head;
channel_t<intptr_t> tail{0};
2019-04-25 09:16:06 +08:00
for (int i = 0; i < MaxNum; ++i)
{
tail = channel_t<intptr_t>{ 1 };
go passing_next(in, tail);
2019-04-25 09:16:06 +08:00
in = tail;
}
2020-04-18 11:46:29 +08:00
#if defined(__GNUC__)
go passing_loop_all(head, tail);
#else
2019-04-25 09:16:06 +08:00
GO
{
2020-03-09 17:59:07 +08:00
for (int i = 0; i < LoopCount; ++i)
2019-04-25 09:16:06 +08:00
{
auto tstart = high_resolution_clock::now();
co_await (head << 0);
intptr_t value = co_await tail;
2019-04-25 09:16:06 +08:00
auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
std::cout << value << " cost time " << dt << "s" << std::endl;
}
};
2020-04-18 11:46:29 +08:00
#endif
2019-04-25 09:16:06 +08:00
this_scheduler()->run_until_notask();
}
2021-11-01 17:59:08 +08:00
int main()
{
benchmark_main_channel_passing_next();
return 0;
}