当前位置: 首页 > news >正文

上代码演示下Profile-Guided Optimization (PGO)

Shell脚本名叫step

#!/bin/bashif [[ $# -ne 1 ]]; then exit; firun() { C="g++ $1 main.cpp"; echo $C; $C; a.out; }case $1 in
'0') run '' ;;
'1') run '-fprofile-generate=.' ;;
'2') run '-fprofile-use=.' ;;
# -march=native不是默认选项,需显式指定,其作用是根据当前编译机器
# 的CPU自动启用支持的指令集优化。
'3') run '-O3 -march=native' ;;
esac

main.cpp

#include <chrono> // C++11时间库
#include <random>
#include <iostream>
using namespace std;auto now = chrono::high_resolution_clock::now;// 模拟热点分支(PGO优化重点)
int process (int x) {if (x > 100) // 该条件90%概率成立(模拟热点分支)return x * 2;return x + 1;
}int main () {random_device rd;auto seed = rd();mt19937 gen(seed);//生成伯努利分布随机布尔值,90% truebernoulli_distribution dist(0.9);auto start = now();int sum = 0;for (int i = 0; i < 1'000'000; ++i)sum += process(dist(gen) ? 150 : 50);// F U C K !cout << chrono::duration<double>(now() - start).count() << "s\n";exit(sum);
}

运行 (Intel N100)

~/pgo$ step 0
g++ main.cpp
0.0532354s
~/pgo$ step 1
g++ -fprofile-generate=. main.cpp
0.060655s
~/pgo$ step 2
g++ -fprofile-use=. main.cpp
0.0532936s
~/pgo$ step 3
g++ -O3 -march=native main.cpp
0.00564738s

colinsblog说:I experimented with some existing C++ 14 applications I’ve written. One, a flat-file to Parquet format converter, improved by only about five percent over an executable built with blanket -O3 optimization levels. Another, the “DCP” I’ve discussed before, improved by around thirty percent faster compared with the same program built with -O3. These tests were done with GCC 5.4, not exactly the newest. I’ll attempt to do similar tests with GCC 7 and 9.

我用的是gcc version 12.2.0 (Debian 12.2.0-14)

AI说:PGO是一种编译器优化技术,通过分析程序实际运行数据(如函数调用频率、分支路径等)生成配置文件,指导编译器对热点代码进行精准优化。其核心流程包括:插桩编译→数据采集→二次优化编译。PGO能显著提升性能,例如减少分支预测错误、优化代码布局以提高缓存命中率,在Chrome等应用中实现10%-20%的性能提升。该技术适用于计算密集型场景,需配合代表性数据收集以发挥最大效果。

AI好啊,讲PGO的中文网页基本上没有代码,而AI能编演示程序,写Makefile,虽然把-fprofile-generate和use的参数当成了文件名,导致use时说找不到文件。

补丁:step 1前得先rm *.gcda.

改成15'000'000后,0.799088s, 0.906551s, 0.800046s, 0.0843707s

http://www.rkmt.cn/news/17918.html

相关文章:

  • day008
  • IRB-120机械臂socket通信接受上位机指令运行程序段
  • tornado异步操作数据库-mysql
  • 实用指南:制冷剂中表压对应温度值的获取(Selenium)
  • Git克隆项目运行指南
  • OpenCV——批量读取可视化图片 - 指南
  • 各种B站客户端
  • CSP-S模拟27
  • 模型训练技巧 - -一叶知秋
  • WPF mvvm datagrid export as pdf via iTextSharp
  • 日总结 9
  • kettle插件-国产数据库瀚高插件,助力国产数据库腾飞
  • 实用指南:provthrd.dll propsys.dll profsvc.dll profprov.dll procinst.dll prntvpt.dll prnntfy.dll
  • 37 ACwing 298 Fence 题解
  • 35 ACwing 297 The Battle Chibi 题解
  • 一款由网易出品的免费、低延迟、专业的远程控制软件,支持手机、平板、Mac 、PC、TV 与掌机等多设备远控电脑!
  • aardio跨窗口传递变量
  • AI在简单视觉推理谜题中的挑战
  • new day
  • MyBatis-Plus 的 QueryWrapper 应用以及在内存中处理JSON数组字符串匹配
  • 从 ZooKeeper 到 ELK:分布式中间件与日志分析系统全解析 - 教程
  • 【MySQL学习笔记】数据库的CURD(一) - 详解
  • fp16训练神经网络时出现nan问题
  • newDay07
  • 余弦日记
  • 关于jinja2的ssti模版注入的学习+过滤
  • [EGOI 2023] Guessing Game
  • [ROI 2018] Addition without carry
  • 解码Linux基础命令
  • 基于 C++ 的高雷诺数湍流直接数值模拟求解器设计与性能优化 - 实践