Page 1 of 1
Performance question [SOLVED]
Posted: Fri May 10, 2019 3:03 pm
by test
In update function, should I write this
or
Code: Select all
if vx ~= 0 then x = x + vx * dt end
? Which is the fastest code?
And not about this question,
why
Code: Select all
v.x, v.y = v.x + v.vx * dt, v.y + v.vy * dt
gives error?
Re: Performance question
Posted: Fri May 10, 2019 7:40 pm
by zorg
The answer to the first question is that it doesn't matter.
The answer to the second needs me to ask for the exact error instead, since what you have there should not error at all (with some assumptions, like v being a table, and the fields vx and vy existing as well as x and y)
Re: Performance question
Posted: Fri May 10, 2019 9:24 pm
by raidho36
To answer your first question, version 2 is actually slower. The check isn't free and neither is branching, a trivial operation like this takes just as much time to complete as does the check. In addition to that, branch prediction failures will incur heavy performance losses whenever should they occur (they occurs when the branch isn't always taken in the same direction). In this specific example the branch depends on the variable being operated upon, so no extra memory lag penalty is incurred, but if it has to check an unrelated value then it might tank the performance if it isn't already in the CPU cache, because RAM is actually very slow to fetch data from. This will also place additional burden on the JIT compiler, because at first it will compile whichever path was taken, and then it'll have to patch it with the other path which might be a suboptimal memory layout - once that compiles, and until then it runs in interpreted mode which is far slower yet.