-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.cpp
85 lines (70 loc) · 1.55 KB
/
demo.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <chrono>
#include <thread>
#include <cmath>
#include "Profiler.h"
double foo(int m) {
PROFILE_ME;
double sum = 0;
for (int i = 0; i < 1000000; i++) {
sum += std::cos(42. * m * i * i);
}
for (int i = 0; i < 100000; i++) {
PROFILE_ME_AS("nop");
}
return std::abs(sum);
}
double bar(int delay_us) {
PROFILE_ME;
std::cout << "[bar] Sleeping for " << delay_us << "us twice" << std::endl;
std::this_thread::sleep_for(std::chrono::microseconds(delay_us));
PROFILE_END; // Early profile stop for bar
std::this_thread::sleep_for(std::chrono::microseconds(delay_us));
}
int fibb(int n) {
PROFILE_ME;
std::this_thread::sleep_for(std::chrono::microseconds(1000));
if (n == 0 || n == 1)
return 1;
return fibb(n-1) + fibb(n-2);
}
int rec2(int n);
int rec1(int n) {
PROFILE_ME;
std::this_thread::sleep_for(std::chrono::microseconds(100));
if (n == 0 || n == 1)
return 1;
return rec1(n-1) + rec2(n-2);
}
int rec2(int n) {
PROFILE_ME;
std::this_thread::sleep_for(std::chrono::microseconds(100));
if (n == 0 || n == 1)
return n + 1;
return rec2(n-1) + rec1(n-2);
}
int main() {
PROFILE_ME;
double total = 0;
{
PROFILE_ME_AS("foo for loop");
for (int m = 1; m <= 10; m++)
total += foo(m);
}
{
PROFILE_ME_AS("bar wait");
bar(10 * total);
}
{
PROFILE_ME_AS("fibb");
fibb(10);
}
{
PROFILE_ME_AS("rec1 + rec2");
rec1(10);
}
PROFILE_END; // Early profile stop to finalize results for main
// std::cout << profiler::getInstance() << std::endl;
profiler::getInstance().print(std::cout, 60);
return 0;
}