%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90%E5%A4%87%E5%BF%98%E5%BD%95/ #24
Replies: 1 comment 1 reply
-
|
看到perf就头大 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90%E5%A4%87%E5%BF%98%E5%BD%95/
Perf
Debian 安装 perf
1
sudo apt install linux-perf
收集分支预测事件
1
perf stat -e branch-instructions,branch-misses <your_program>
branch-instructions:分支指令的总数
branch-misses:分支预测失败的次数
1
hit_rate = 1 - branch-misses / branch-instructions //命中率
以下是一个包含多个复杂分支跳转的 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 模拟复杂条件的函数
int complex_condition(int x) {
if (x % 2 == 0) {
if (x % 3 == 0) {
return 1; // 可被2和3整除
} else {
if (x % 5 == 0) {
return 2; // 可被2和5整除
} else {
return 3; // 可被2整除,但不能被3或5整除
}
}
} else {
if (x % 7 == 0) {
return 4; // 可被7整除
} else {
return 5; // 不能被2或7整除
}
}
}
// 递归函数,模拟复杂的分支跳转
int recursive_function(int depth) {
if (depth <= 0) {
return 0;
} else {
if (depth % 2 == 0) {
return depth * recursive_function(depth - 1);
} else {
return depth + recursive_function(depth - 1);
}
}
}
// 主函数,包含多重循环和分支判断
int main() {
srand(time(NULL)); // 用当前时间作为随机数种子
}
编译之后,使用 perf 进行分支预测事件统计得到结果:
https://hcy-asleep.github.io/%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90%E5%A4%87%E5%BF%98%E5%BD%95/
Beta Was this translation helpful? Give feedback.
All reactions