Optimize C++ performance with Profile-Guided Optimization and Google Benchmark
Introduction
Profile-Guided Optimization
Google Benchmark
Example operation
Using Profile Guided Optimization
Incorporating PGO into a GitHub Actions workflow
Next Steps
Optimize C++ performance with Profile-Guided Optimization and Google Benchmark
Who is this for?
Developers looking to optimize C++ performance based on runtime behavior.
What will you learn?
Upon completion of this Learning Path, you will be able to:
- Microbenchmark a function using Google Benchmark.
- Apply profile-guided optimization to build performance-tuned binaries.
Prerequisites
Before starting, you will need the following:
- Basic C++ understanding
- Access to an Arm-based Linux machine, such as an AWS Graviton-based instance or an Arm AGI CPU platform
Summary
This summary was drafted with an approved AI-assisted workflow and reviewed by Arm contributors before publication. Human technical review remains part of the process so the final page reflects engineering rigor, accuracy, and Arm editorial standards.
You’ll apply profile-guided optimization (PGO) to a C++ microbenchmark on an Arm-based Linux system with Google Benchmark. You’ll build an instrumented binary with
-fprofile-generate, run it to create .gcda files, and recompile with -fprofile-use. You’ll compare benchmark timings before and after PGO, inspect the generated artifacts, and integrate PGO into either a Makefile or a continuous integration Github Actions workflow.Frequently asked questions
These FAQs were drafted with an approved AI-assisted workflow and reviewed by Arm contributors before publication. Human technical review remains part of the process so the final page reflects engineering rigor, accuracy, and Arm editorial standards.
Build the instrumented binary with
-fprofile-generate, run it to collect runtime data, then rebuild with -fprofile-use. This two-step cycle enables the compiler to use the collected profile during optimization.The run generates profile data files with a
.gcda extension in the same directory. Use these files as inputs for the subsequent -fprofile-use build.Google Benchmark runs managed iterations and reports timing results for the benchmarked function. You should see timing output you can compare across the non-PGO and PGO builds.
Verify that you ran the instrumented binary and that
.gcda files exist in the build directory. Then confirm that the recompile used -fprofile-use in the same location where the the run generated the profile files.Use PGO for performance-critical code that runtime behavior strongly influences. Avoid applying it broadly to early-stage code or highly variable workloads because the extra build steps add time and may not yield stable benefits.