Performance question [SOLVED]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
test
Prole
Posts: 28
Joined: Sun Apr 14, 2019 2:36 pm

Performance question [SOLVED]

Post by test » Fri May 10, 2019 3:03 pm

In update function, should I write this

Code: Select all

x = x + vx * dt
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?
Last edited by test on Fri May 17, 2019 9:56 am, edited 1 time in total.

User avatar
zorg
Party member
Posts: 2745
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Performance question

Post by zorg » Fri May 10, 2019 7:40 pm

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)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
raidho36
Party member
Posts: 1987
Joined: Mon Jun 17, 2013 12:00 pm

Re: Performance question

Post by raidho36 » Fri May 10, 2019 9:24 pm

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.

Post Reply

Who is online

Users browsing this forum: drfractal and 8 guests