Const vs. Static

Hello,

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?

Static MY_VALUE As Integer = 100

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?

For well documented languages I would give an recommendation, or ask you to look in the Assembler code.

But in Xojo… I would recommend to call your routine hundreds of thousands of times and measure the runtime several times and compare them.

Absolutely. Static has runtime overhead but Const is guaranteed not to.