## Performance question [SOLVED]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
test
Prole
Posts: 28
Joined: Sun Apr 14, 2019 2:36 pm

### Performance question [SOLVED]

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?
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.

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

### Re: Performance question

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 True 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.

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Performance question

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.

### Who is online

Users browsing this forum: Google [Bot] and 37 guests