15 #if defined(LIBCOPP_MACRO_ENABLE_STD_COROUTINE) && LIBCOPP_MACRO_ENABLE_STD_COROUTINE
17 # if defined(PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO) && PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO
19 # define CALC_CLOCK_T std::chrono::system_clock::time_point
20 # define CALC_CLOCK_NOW() std::chrono::system_clock::now()
21 # define CALC_MS_CLOCK(x) static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(x).count())
22 # define CALC_NS_AVG_CLOCK(x, y) \
23 static_cast<long long>(std::chrono::duration_cast<std::chrono::nanoseconds>(x).count() / (y ? y : 1))
25 # define CALC_CLOCK_T clock_t
26 # define CALC_CLOCK_NOW() clock()
27 # define CALC_MS_CLOCK(x) static_cast<int>((x) / (CLOCKS_PER_SEC / 1000))
28 # define CALC_NS_AVG_CLOCK(x, y) (1000000LL * static_cast<long long>((x) / (CLOCKS_PER_SEC / 1000)) / (y ? y : 1))
31 using benchmark_callable_future_type = copp::callable_future<int64_t>;
32 using benchmark_generator_future_type = copp::generator_future<int64_t>;
34 std::vector<std::unique_ptr<benchmark_callable_future_type>> g_benchmark_callable_list;
35 std::vector<benchmark_generator_future_type::context_pointer_type> g_benchmark_generator_list;
38 int max_task_number = 100000;
40 benchmark_callable_future_type run_benchmark(
size_t idx,
int left_switch_count) {
43 benchmark_generator_future_type generator{[idx](benchmark_generator_future_type::context_pointer_type ctx) {
44 g_benchmark_generator_list[idx] = std::move(ctx);
46 while (left_switch_count-- >= 0) {
47 generator.get_context()->reset_value();
48 auto gen_res = co_await generator;
56 g_benchmark_callable_list.reserve(
static_cast<size_t>(max_task_number));
57 g_benchmark_generator_list.resize(
static_cast<size_t>(max_task_number),
nullptr);
59 printf(
"### Round: %d ###\n", index);
61 time_t begin_time = time(
nullptr);
65 while (g_benchmark_callable_list.size() <
static_cast<size_t>(max_task_number)) {
66 g_benchmark_callable_list.push_back(std::unique_ptr<benchmark_callable_future_type>(
67 new benchmark_callable_future_type(run_benchmark(g_benchmark_callable_list.size(),
switch_count))));
70 time_t end_time = time(
nullptr);
72 printf(
"create %d callable(s) and generator(s), cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
73 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
76 begin_time = end_time;
77 begin_clock = end_clock;
80 bool continue_flag =
true;
81 long long real_switch_times =
static_cast<long long>(0);
84 while (continue_flag) {
86 continue_flag =
false;
87 for (
auto& generator_context : g_benchmark_generator_list) {
88 benchmark_generator_future_type::context_pointer_type move_context;
89 move_context.swap(generator_context);
91 move_context->set_value(round);
98 end_time = time(
nullptr);
100 printf(
"resume %d callable(s) and generator(s) for %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
101 max_task_number, real_switch_times,
static_cast<int>(end_time - begin_time),
104 begin_time = end_time;
105 begin_clock = end_clock;
107 g_benchmark_callable_list.clear();
108 g_benchmark_generator_list.clear();
110 end_time = time(
nullptr);
112 printf(
"remove %d callable(s), cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
113 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
117 int main(
int argc,
char* argv[]) {
118 puts(
"###################### std callable - reuse generator - trivial ###################");
119 printf(
"########## Cmd:");
120 for (
int i = 0; i < argc; ++i) {
121 printf(
" %s", argv[i]);
126 max_task_number = atoi(argv[1]);
133 for (
int i = 1; i <= 5; ++i) {
140 puts(
"std coroutine is not supported by current compiler.");
#define CALC_NS_AVG_CLOCK(x, y)
static void benchmark_round(int index)