I have a method called hundreds of thousands of times. It contains a fixed, unchangeable value. I’ve decided to create a Constant.
Const MY_VALUE = 100
This Constant is used to calculate other values. Now I wonder whether the Constant for the many calls of the method is created again and again? If so, isn’t it more resource-saving to create them as Static to speed up the method?
I bet that this will be optimized away in any case, even without Const.
Static would slow down things in this case, because this will force the compiler to reserve memory (in the file section) and load this variable into cache every time instead of using assembler code with a fixed value.
[quote=384807:@G. Deppendorf]I bet that this will be optimized away in any case, even without Const.
Static would slow down things in this case, because this will force the compiler to reserve memory (in the file section) and load this variable into cache every time instead of using assembler code with a fixed value.[/quote]
Thank you for your contribution. So I read you correctly that you recommend the use of the constant?