Currently i'm working on micro-optimisation of my gamemode.
I didn't found anything, so i'm asking here: Which code runs faster?
if not t.val then
t.val = {}
end
or
t.val = t.val or {}
In addition to this, focussed on improving expressions (lua-users wiki), is there any speed difference between
y = x + 1 + 2
and (unnecessary parentheses)
y = x + (1 + 2)
Thanks!
That makes no difference in terms of speed.
It's even mentioned on the Logical Operators page that:
A useful Lua idiom is x = x or v
, which is equivalent to
if not x then x = v end
Not sure why you think parentheses would cause any slow down.
Dont bother yourself with microoptimisation, as it reduces code readability, maintainability, etc. rather, optimize what takes up the most time. FProfiler is a good tool for that.
hmmm... I tested on my own and did some other searches. There i found the following: https://springrts.com/wiki/Lua_Performance Test 5 concludes that the second way will be a little bit faster. I know that code gets compiled, and maybe the compiled code will be the same. And that's the question - more or less. But your answers compared with the test differs...
@polivilas sure, i know the difference between micro- and macro-optimization. But that was not my question. I wouldn't ask if I did not care ^^ And why not improving the code if it's possible and does not reduce the readability very much. All in all, you are right. ^^
You really can't use that site you linked for actual assumptions. Garry's Mod is NOT running just Lua. It uses Lua JIT which does a ton of optimizations on it's own which may or may not make many of those performance tests not true.
That's implying all microoptimisations reduce code readability as opposed to a lateral semantic change, or performance-intensive code that relies on each instruction save doesn't exist (maybe less so in Lua but the philosophy still applies).
LuaJIT specifically inlines "or" statements that act like a ternary, specifically when it results in a constant operation (table creation).
Don't bother with micro-optimization, focus on stuff that can bring major performance improvements.
Sorry, you need to Log In to post a reply to this thread.