Verified answer: value is calculated every time D:
I used the test shown in my comment above and this is the results from my performance project, in built release:
{
"name": "Add two constants"
"time": 23,
},
{
"name": "Add 20 constants"
"time": 267,
}
As you can see, adding 20 constants is much slower than adding 2. But in reality this should not be different since it's just constants added together.
I think the reason why it's not precalculated is that the result is stored in a var
, and the optimizer is not clever enough to merge the additions into a simple assignment.
FYI, the two tests I added:
#-------------------------------------------------------
const CONST01 = 0
const CONST02 = 0
func test_constants_addition():
start("Add two constants")
for i in range(0,ITERATIONS):
var x = CONST01 + CONST02
stop()
#-------------------------------------------------------
const CONST03 = 0
const CONST04 = 0
const CONST05 = 0
const CONST06 = 0
const CONST07 = 0
const CONST08 = 0
const CONST09 = 0
const CONST10 = 0
const CONST11 = 0
const CONST12 = 0
const CONST13 = 0
const CONST14 = 0
const CONST15 = 0
const CONST16 = 0
const CONST17 = 0
const CONST18 = 0
const CONST19 = 0
const CONST20 = 0
func test_constants_addition_extended():
start("Add 20 constants")
for i in range(0,ITERATIONS):
var x = CONST01 + CONST02 + CONST03 + CONST04 + CONST05 + CONST06 + CONST07 + CONST08 + CONST09 + CONST10 + \
CONST11 + CONST12 + CONST13 + CONST14 + CONST15 + CONST16 + CONST17 + CONST18 + CONST19 + CONST20
stop()