-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibers-ctx_bench.cpp
48 lines (35 loc) · 1.1 KB
/
fibers-ctx_bench.cpp
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
#include <thread>
#include <iostream>
#include <time.h>
#include <vector>
#include <boost/fiber/all.hpp>
#include "timers.h"
constexpr uint64_t iterations = 100000000;
void func1() {
for (int i = 0; i < iterations*2; i++) {
boost::this_fiber::yield();
}
}
void func2() {
struct timespec ts;
auto start_ns = time_ns(&ts);
auto start = rdtsc();
for (int i = 0; i < iterations; i++) {
boost::this_fiber::yield();
}
auto delta_cycles = rdtsc() - start;
auto delta = time_ns(&ts) - start_ns;
constexpr int nswitches = iterations << 2;
fmt::print("{}\tctx_switches={}\ttime={}ns\t{}ns/ctxsw\tcycles={}\tavg_lat={}cycles/ctx\n", __func__,
nswitches, delta, (delta / (float) nswitches), delta_cycles, delta_cycles/nswitches);
}
int main(int argc, char* argv[]) {
std::vector<boost::fibers::fiber> fibers;
int nb_fibers = 2;
fibers.emplace_back(boost::fibers::launch::post, func1);
fibers.emplace_back(boost::fibers::launch::post, func2);
std::cout << "fibers launched .. good luck!\n";
for (auto& fiber : fibers)
fiber.join();
std::cout << "all fibers joined\n";
}