Using 2.2.5 stable
test snippet
Code:::func <- function() {
return 251*139
}
::T1 <- {T2 = {T3 = {T4 = {func = ::func}}}}
local freefunc = ::func
T1.test <- function():(freefunc) {
local start = clock()
local f = ::func
for (local i = 0; i < 10000000; i++){
f()
}
print(clock() - start)
}
T1.test()
I noticed that in some cases calling functions explicitly using :: is slower than not using it. I find this weird, as I imagine not using it would look through local -> constant -> environment -> root, but using it would skip all directly to root.
However, not calling a function but just looking up (removing '()') does indeed work as intended, with :: being faster in every case.
Using free variables are also slower than using local variables in every case. I thought using a free variable would lookup once, bind the value of the func to the function in compile, and use it as a local var (implicit param) - compared to using a local variable and looking up every time it is called (though I realize it is just once and should not make any difference).
Changing inside the loop, here are my consistent test results
Code:
f() : 1.070 : local var
freefunc() : 1.170 : free var
func() : 1.430 : global, no prefix
::func() : 1.320 : global, with :: faster
T1.T2.T3.T4.func() : 2.080 : global, no prefix
::T1.T2.T3.T4.func() : 2.280 : global, with :: slower
Is this normal?
Also while I know such differences would not matter in most cases, under 2000 loops or whatever, wouldn't it make sense to assign global lookups to local variables every time?