Boost C++ Performance: 20% Gains with gprof Profiling and Smart Optimizations
This article explains how to apply the Pareto principle and gprof profiling to identify and fix costly C++ code patterns—such as large object initialization and inefficient map usage—resulting in up to a 10% overall performance improvement with minimal code changes.
Optimization Guidelines
1. Pareto principle: focus on the ~20% of code that consumes most time. Example: function A is large but called once; function B is small but called 1000 times, so prioritize B.
2. Write code first, optimize later; premature optimization can hurt readability and development efficiency.
Tools
gprof – GNU profiling tool for C/C++ on Linux, AIX, Sun etc. It records function call counts, CPU time, and call graphs.
Using gprof
1) Compile with -pg flag, e.g., g++ -pg -o test.exe test.cpp.
2) Run the executable; it generates gmon.out with profiling data.
3) Analyze with gprof test.exe gmon.out (or redirect output to a file).
Practice
Faced with performance bottlenecks, we used gprof and identified two major issues.
1. Expensive Large Object Initialization
Object VOBJ1 was constructed 307 times, consuming 6.5% of total time. Moving initialization to the constructor initializer list reduced its cost to 1.8%.
2. Inefficient Map Usage
Recordset::GetField called 89 times, costing 6.8%. The original code used map["a"] which inserts missing keys. Replacing with find and switching from map (red‑black tree) to unordered_map cut the cost to 1.4% and halved execution time.
string Recordset::GetField(const string &strName)
{
unordered_map::iterator iter = m_fields.find(strName);
if (iter == m_fields.end())
{
LOG_ERR("[Recordset::GetField] " << strName << " Not Find!!");
}
return m_records[nIndex].GetValue(iter->second - 1);
}Summary
By modifying fewer than 30 lines of code, overall performance improved by about 10%. Targeted profiling and focusing on the critical hotspots—like heavy object construction and suboptimal container choices—delivers significant gains.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
