Welcome Guest Search | Active Topics | Log In | Register

Performance on variable lookup
John Smith
#1 Posted : Friday, May 8, 2020 9:24:05 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/8/2020(UTC)
Posts: 1

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Using 2.2.5 stable

test snippet

::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++){

    print(clock() - start)


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


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?
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.056 seconds.