Guide
Using GCC Profile-Guided Optimization to Boost Runtime Performance
Learn how to enable GCC's profile-guided optimization with -fprofile-generate and -fprofile-use, see a simple example, and avoid common pitfalls.
Published by Tasadduq Burney
19 Aug 2025, 11:09 UTC
2 min121K views0

Why use GCC profile-guided optimization?
Enabling GCC's profile-guided optimization (PGO) can give a typical 5-15% speedup for many applications by letting the compiler inline hot functions, lay out code for better branch prediction, and adjust optimizations based on actual execution counts.
Worked example
Sample program
#include
int main() {
long sum = 0;
for (long i = 0; i < 1000000000L; ++i) {
sum += i & 1;
}
printf("%ld\n", sum);
return 0;
}
Build and run steps
- Compile with instrumentation:
gcc -fprofile-generate -O2 -o prog_train prog.c - Run with representative input (the program uses no input, so just execute):
./prog_train - Recompile using the gathered profile:
gcc -fprofile-use -O2 -o prog_opt prog.c - Run the optimized binary and measure:
time ./prog_opt
Limits and common mistakes
- The training run must resemble the production workload; otherwise the profile can misguide optimizations and even slow down the binary.
- Old *.gcda and *.gcno files must be removed between builds, otherwise the compiler may merge stale data with fresh runs.
- Using different compiler options (especially optimization level) between the -fprofile-generate and -fprofile-use steps can lead to inconsistent profiles.
- Code that depends on nondeterministic inputs (e.g., random seeds, timestamps) produces a profile that does not reflect a fixed execution path, making PGP unreliable.
- Combining PGO with aggressive link-time optimization (-flto) can increase link time and binary size; test the combination before adopting it in a release build.
Verification
To check that the profile was applied:
- Ensure the *.gcda files generated during the training run are non‑zero (e.g.,
ls -l *.gcda). - Compare execution times of the instrumented and optimized builds with
timeorperf stat; a reduction in branch‑miss counters measured byperfindicates successful PGO. - The final binary size should not grow dramatically; a modest size change is normal, but a large increase may signal a problem.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.