-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.cc
50 lines (41 loc) · 1.24 KB
/
benchmark.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
39
40
41
42
43
44
45
46
47
48
49
50
//
// Created by xvvx on 18-12-2.
//
#include "coroutine.h"
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <thread>
constexpr uint32_t loop_times = 500 * 100;
uint64_t coroutine_test() {
ecoroutine::CoroutineFunc func = [](){};
clock_t start = clock();
for (int i = 0; i < loop_times; ++i) {
ecoroutine::coroutine_t coroutine1 = ecoroutine::create(func);
ecoroutine::coroutine_t coroutine2 = ecoroutine::create(func);
ecoroutine::run(coroutine1);
ecoroutine::run(coroutine2);
}
clock_t end = clock();
return end - start;
}
uint64_t thread_test() {
auto func = [](){};
clock_t start = clock();
for (int i = 0; i < loop_times; ++i) {
std::thread thread1(func);
std::thread thread2(func);
thread1.join();
thread2.join();
}
clock_t end = clock();
return end - start;
}
int main(int argc, char* argv[]) {
const int kMicrosecondsPerSecond = 1000 * 1000;
printf("coroutine: %d times %f s\n", loop_times * 2,
static_cast<double>(coroutine_test()) / kMicrosecondsPerSecond);
printf("thread: %d times %f s\n", loop_times * 2,
static_cast<double>(thread_test()) / kMicrosecondsPerSecond);
return 0;
}