Prioritize algorithmic efficiency and avoid unnecessary computational overhead in performance-critical code paths. Look for opportunities to reduce time complexity and eliminate redundant calculations.
Prioritize algorithmic efficiency and avoid unnecessary computational overhead in performance-critical code paths. Look for opportunities to reduce time complexity and eliminate redundant calculations.
Key optimization strategies:
prefer distanceSq
over distance()
for performance-critical geometric calculationsstd::erase_if
directly instead of the std::ranges::remove_if
+ erase
pattern when possibleExample from distance calculations:
// Instead of:
if (m_vBeginDragXY.distance(mousePos) <= *PDRAGTHRESHOLD)
// Use:
if (m_vBeginDragXY.distanceSq(mousePos) <= (*PDRAGTHRESHOLD * *PDRAGTHRESHOLD))
Example from workspace checking:
// Instead of checking all workspaces against all rules O(n*m):
for (auto& workspace : workspaces) {
for (auto& rule : rules) { /* check */ }
}
// Optimize to O(n) by checking only the changed workspace:
recheckPersistent(); // Only checks current workspace against rules
This approach reduces CPU cycles in frequently called functions and improves overall application responsiveness.
Enter the URL of a public GitHub repository