In Lua, should I define a variable every iteration of a loop or before the loop?

后端 未结 1 1789
清酒与你
清酒与你 2021-01-13 05:32

Specifically in Lua, will I do any harm by doing this:

for i = 1, 10 do
    local foo = bar()
    -- do stuff with foo
end

instead of this:

相关标签:
1条回答
  • 2021-01-13 05:45

    Go for the safest alternative, which is to use the smallest scope for all variables. As for efficiency, local variables are stored in a stack; no memory allocation is done inside the loop.

    0 讨论(0)
提交回复
热议问题