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 while (left_switch_count-- >= 0) {
45 co_await benchmark_generator_future_type([idx](benchmark_generator_future_type::context_pointer_type ctx) {
46 g_benchmark_generator_list[idx] = std::move(ctx);
55 g_benchmark_callable_list.reserve(
static_cast<size_t>(max_task_number));
56 g_benchmark_generator_list.resize(
static_cast<size_t>(max_task_number),
nullptr);
58 printf(
"### Round: %d ###\n", index);
60 time_t begin_time = time(
nullptr);
64 while (g_benchmark_callable_list.size() <
static_cast<size_t>(max_task_number)) {
65 g_benchmark_callable_list.push_back(std::unique_ptr<benchmark_callable_future_type>(
66 new benchmark_callable_future_type(run_benchmark(g_benchmark_callable_list.size(),
switch_count))));
69 time_t end_time = time(
nullptr);
71 printf(
"create %d callable(s) and generator(s), cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
72 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
75 begin_time = end_time;
76 begin_clock = end_clock;
79 bool continue_flag =
true;
80 long long real_switch_times =
static_cast<long long>(0);
83 while (continue_flag) {
85 continue_flag =
false;
86 for (
auto& generator_context : g_benchmark_generator_list) {
87 benchmark_generator_future_type::context_pointer_type move_context;
88 move_context.swap(generator_context);
90 move_context->set_value(round);
97 end_time = time(
nullptr);
99 printf(
"resume %d callable(s) and generator(s) for %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
100 max_task_number, real_switch_times,
static_cast<int>(end_time - begin_time),
103 begin_time = end_time;
104 begin_clock = end_clock;
106 g_benchmark_callable_list.clear();
107 g_benchmark_generator_list.clear();
109 end_time = time(
nullptr);
111 printf(
"remove %d callable(s), cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
112 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
116 int main(
int argc,
char* argv[]) {
117 puts(
"###################### std callable - create generator - trivial ###################");
118 printf(
"########## Cmd:");
119 for (
int i = 0; i < argc; ++i) {
120 printf(
" %s", argv[i]);
125 max_task_number = atoi(argv[1]);
132 for (
int i = 1; i <= 5; ++i) {
139 puts(
"std coroutine is not supported by current compiler.");
#define CALC_NS_AVG_CLOCK(x, y)
static void benchmark_round(int index)