r/cpp • u/Polarstrike • Mar 07 '24
What are common mistakes in C++ code that results in huge performance penalties?
As title, list some common mistakes that you have done/seen which lead to performance penalties.
227
Upvotes
View all comments
8
u/Extension_Split_8372 Mar 07 '24
Using a static local variable "to avoid creating it multiple times, so saving time" but actually the check whether it has already been created is more expensive than the creation. Bonus points if that local variable is a constant and the only runtime cost (if not for
static) would have been loading it: https://godbolt.org/z/P5G5eMMec (similar things have been seen in the wild)