-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchtest.cc
38 lines (31 loc) · 854 Bytes
/
benchtest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "coropp.h"
#include <stdio.h>
#include <chrono>
using namespace coro;
using namespace std::chrono;
const int kBenchNum = 1000 * 1000;
void foo(int base)
{
for (int i = 0; i < kBenchNum; ++i) {
printf("coroutine %d: %d\n", co_running_id(), base + i);
co_yield();
}
}
int main()
{
int co1 = co_new([]{ foo(0); });
int co2 = co_new([]{ foo(100); });
printf("main start\n");
auto start = system_clock::now();
while (co_alive(co1) && co_alive(co2)) {
co_resume(co1);
co_resume(co2);
}
auto end = system_clock::now();
auto interval = duration_cast<nanoseconds>(end-start);
double total_time = double(interval.count()) / (1000 * 1000 * 1000);
int unit = interval.count() / (2 * 1000 * 1000);
printf("total time = %f s, time per switch = %d ns\n", total_time, unit);
printf("main end\n");
return 0;
}