Examining how the final keyword affects the efficiency of C++ programs

Benjamin Summerton, author of the ray tracing system PSRayTracing, analyzed impact on application performance when using the keyword ” in C++ codefinal“, which appeared in the C++11 standard. The reason for testing was that there were claims floating around the Internet that using “final” would improve performance, which were limited to value judgments without indicating the results of the changes.

Benjamin's testing has shown that performance when using “final” is highly compiler dependent. When building in GCC, performance actually increased in a noticeable number of cases, but when building in Clang and MSVC, performance in most cases decreased, and more noticeably. At the same time, in addition to the compiler, the platform had a great influence; for example, performance degradation was more pronounced on a system with an AMD Ryzen 9 6900HX CPU than on a system with an Apple M1 CPU.

Advertisement

For example, on an AMD Ryzen 9 6900HX system with Ubuntu 23.10, when building in Clang, 90% of tests when using “final” showed a slowdown of at least 5%, but in 2.5% of cases a speedup of at least 5% was recorded. For GCC, a 5% slowdown was recorded in 0.9% of cases, and a 5% acceleration in 15.8% of cases. In MSVC, 5% slowdown was observed in 26.2% of tests, and 5% acceleration was observed in 13.3%. For himself, the author of the study concluded that it is necessary to avoid using “final”.

Thanks for reading:

Advertisement

Advertisement