cppcheck often recommends that variables be moved deeper into code blocks such as FOR and WHILE loops. This is actually bad form, as the declaration becomes much more expensive to perform inside the loop than before the loop. Recommending that variables be moved inside IF / ELSE / ELSE IF blocks is helpful for C99+ code, but doing the same for FOR / WHILE blocks encourages poor performance.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have searched and read a bit and i am somewhat with you. There could be cases in which it can cost performance when a variable is declared inside a loop. That is particularly true for C++ when a class with a heavy constructor is instantiated again and again inside a loop.
But i also read some comments about positive effects (also wrt. performance) when variables are declared inside a loop.
And as it is often hard to say what the compiler exactly does/optimizes there is not the one and only solution. When it comes to performance it is normally necessary to profile the program and use this information to optimize it.
If you have some good sources about that topic could you share them?
Maybe it is enough to extend the message from Cppcheck to warn that it could cost performance when a (complex) variable is declared inside a loop?
Or maybe it makes sense to mark the message as inconclusive when the scope of the variable will be inside a loop?
Any other suggestions?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In general reducing variable scopes even for loops improves register allocation and live variable analysis which can help improve performance. This probably doesn't apply as much to non-trivial types, but I believe cppcheck doesn't warn about these type.
Here is some articles I found about the issue, which all seem to state that it good practice to declare variables inside the loop where possible:
cppcheck often recommends that variables be moved deeper into code blocks such as FOR and WHILE loops. This is actually bad form, as the declaration becomes much more expensive to perform inside the loop than before the loop. Recommending that variables be moved inside IF / ELSE / ELSE IF blocks is helpful for C99+ code, but doing the same for FOR / WHILE blocks encourages poor performance.
I have searched and read a bit and i am somewhat with you. There could be cases in which it can cost performance when a variable is declared inside a loop. That is particularly true for C++ when a class with a heavy constructor is instantiated again and again inside a loop.
But i also read some comments about positive effects (also wrt. performance) when variables are declared inside a loop.
And as it is often hard to say what the compiler exactly does/optimizes there is not the one and only solution. When it comes to performance it is normally necessary to profile the program and use this information to optimize it.
If you have some good sources about that topic could you share them?
Maybe it is enough to extend the message from Cppcheck to warn that it could cost performance when a (complex) variable is declared inside a loop?
Or maybe it makes sense to mark the message as
inconclusive
when the scope of the variable will be inside a loop?Any other suggestions?
In general reducing variable scopes even for loops improves register allocation and live variable analysis which can help improve performance. This probably doesn't apply as much to non-trivial types, but I believe cppcheck doesn't warn about these type.
Here is some articles I found about the issue, which all seem to state that it good practice to declare variables inside the loop where possible:
https://stackoverflow.com/a/7959658/375343
https://softwareengineering.stackexchange.com/a/296724/150182
https://www.quora.com/Is-it-better-to-declare-a-repeatedly-used-variable-inside-a-loop-or-outside