forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiling.c
196 lines (160 loc) · 3.31 KB
/
profiling.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
Some functions to play with profiling.
You likely want to do assembly analysis as well when profiling things.
- turn off optimization if you want results to make evident sense
- even without optimization, cache access speed is hard to predict
so what you expect may be false
*/
#include "common.h"
static const int n_prof_runs = 100000000;
/* Only the loop. */
/* Discount this from every other profile run */
void loop_only_prof(int n) {
int i;
for(i=0; i<n; i++);
}
void while_only_prof(int n) {
int i = 0;
while(i < n)
++i;
}
void int_assign_prof(int n) {
int i,j;
for(i=0; i<n; i++)
j=1;
}
void do_nothing(){}
void func_all_prof(int n) {
int i;
for(i=0; i<n; i++)
do_nothing();
}
static inline void inline_do_nothing(){}
void inline_func_call_prof(int n) {
int i;
for(i=0; i<n; i++)
inline_do_nothing();
}
void int_sum_prof(int n) {
int i, j = 0;
for(i=0; i<n; i++)
j = j + 0;
}
void int_sub_prof(int n) {
int i, j = 0;
for(i=n; i>0; i--);
j = j - 0;
}
void int_mult_prof(int n) {
int i, j = 1;
for(i=0; i<n; i++)
j = j * 1;
}
void int_div_prof(int n) {
int i, j = 1;
for(i=0; i<n; i++)
j = j / 1;
}
void float_sum_prof(int n) {
float f;
int i;
for(i=0; i<n; i++)
f = f + 0.0;
}
void float_sub_prof(int n) {
float f;
int i;
for(i=0; i<n; i++)
f = f - 0.0;
}
void float_mult_prof(int n) {
int i;
float j;
for(i=0; i<n; i++)
j = j * 1.0;
}
void float_div_prof(int n) {
int i;
float j;
for(i=0; i<n; i++)
j = j / 1.0;
}
void putsProf(int n) {
int i;
for(i = 0; i < n; ++i)
puts("a");
}
void stack1b_prof(int n) {
int is[1];
int i;
for(i = 0; i < n; ++i) {
int is[1];
}
}
void stack1kb_prof(int n) {
int is[1];
int i;
for(i = 0; i < n; ++i) {
int is[0x800];
}
}
void stack1mb_prof(int n) {
int is[1];
int i;
for(i = 0; i < n; ++i) {
int is[0xF0000];
}
}
void heap1b_prof(int n) {
char *cp;
int i;
for(i = 0; i < n; ++i) {
cp = (char*) malloc(sizeof(char) * 1);
free(cp);
}
}
void heap1kb_prof(int n) {
char *cp;
int i;
for(i = 0; i < n; ++i) {
cp = (char*) malloc(sizeof(char) * 0x800);
free(cp);
}
}
void heap1mbProf(int n) {
char *cp;
int i;
for(i = 0; i < n; ++i) {
cp = (char*) malloc(sizeof(char) * 0xF0000);
free(cp);
}
}
int main() {
loop_only_prof(n_prof_runs);
while_only_prof(n_prof_runs);
int_assign_prof(n_prof_runs);
int_sum_prof(n_prof_runs);
int_sub_prof(n_prof_runs);
int_mult_prof(n_prof_runs);
int_div_prof(n_prof_runs);
float_sum_prof(n_prof_runs);
float_sub_prof(n_prof_runs);
float_mult_prof(n_prof_runs);
float_div_prof(n_prof_runs);
func_all_prof(n_prof_runs);
inline_func_call_prof(n_prof_runs);
stack1b_prof(n_prof_runs);
stack1kb_prof(n_prof_runs);
stack1mb_prof(n_prof_runs);
heap1b_prof(n_prof_runs);
heap1kb_prof(n_prof_runs);
/* By far the slowest. */
/*heap1mbProf(n_prof_runs);*/
/*
BAD:
Don't do stdout on profiling.
System time is not counted anyways.
*/
/*putsProf(n_prof_runs);*/
return EXIT_SUCCESS;
}