Search code examples
syntaxlua

lua syntax questions regarding micro optimizations


So I'm trying to figure out which would be best overall for performance between syntax usages with lua.

I have 3 examples that all basically do the same thing but I'm wondering in what order they would rank in terms of performance:

ex 1.

local test_var = true
for i = 1,3 do
    if test_var then
        return
    end
    print('test')
end

ex. 2

local test_var = true
for i = 1,3 do
    if test_var then
        break
    end
    print('test')
end

ex. 3

local test_var = false
for i = 1,3 do
    if not test_var then
        return
    end
    print(test)
end

ex. 4

local test_var = false
for i = 1,3 do
    if not test_var then
        goto continue
    end
    print('test')
    ::continue::
end

ex. 5

local test_var = false
for i = 1,3 do
    if test_var then
        print('test')
    end
end

there are probably also a few other variations of how this could be written but basically I'm just looking for what would be the absolute best performance wise. while this is quite simple stuff its being used for I'm planning to reorganize every usage I have so far in alot of scripts I'm running and some do some rather complex work so I'm hoping to improve things as much as I can


Solution

    1. Just don't
    2. Try benchmarking it
    3. Still, just don't

    Seriously, if you're using Lua, you shouldn't be trying to optimize on that level, specially if you don't understand the internals enough to answer those questions for yourself.

    A few hints though:

    • If you're using LuaJIT, don't bother. The compiler is smarter than you and will get the code to run as fast as possible if you just focus on readability and idiomatic Lua without having to do any "optimization" nonesense.
    • If you're using PUC Lua, your use case probably doesn't need crazy performance anyway, otherwise you would be using LuaJIT or a system programming language.
    • Focus on higher-level optimizations instead. Reducing table allocations and function closing gives you way more of a performance increase than the minor changes you're asking about.
    • Be careful with assumptions about the Lua VM. It might seem "intuitive" that goto is faster than a loop construct, because it's a more "low level" thing, but the Lua VM has special instructions for many syntax constructs like for loops, so they may well perform better than plain gotos.
    • Write benchmarks. It's incredibly easy to throw together a simple benchmark in Lua so you really should just be testing for yourself what code runs better in a real-life scenario. Just remember to run your benchmark on several Lua versions.
    • Don't optimize the wrong code.

    And lastly, just move the condition out of the loop.


    PS: A note about asking questions

    When asking about performance, always give realistic numbers or comment their ranges. When variables can change over time, comment it or add some dummy code to change them.

    The way you asked your question, the naive answer would be:

    if test_var then
       print("test") print("test") print("test")
    end
    

    and it wouldn't even be wrong.