Reputation: 23
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
Upvotes: 1
Views: 177
Reputation: 7064
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:
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.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.
Upvotes: 5