if button==2 then
-----------------------------------------------------------------------------------northwest
if LocalX < 1 and LocalY < 1 and LocalX > -64 and LocalY > -64 then
object_chunk_northwest[LocalX+64][LocalY+64]=1; update_objects("northwest"); end
-----------------------------------------------------------------------------------west
if LocalX < 1 and LocalY < 65 and LocalY > 0 and LocalX > -64 then
object_chunk_west[LocalX+64][LocalY]=1; update_objects("west"); end
-----------------------------------------------------------------------------------center
if LocalX > 0 and LocalY > 0 and LocalX <65 and LocalY<65 then
object_chunk[LocalX][LocalY]=1; update_objects("center"); end
-----------------------------------------------------------------------------------north
if LocalX > 0 and LocalY < 1 and LocalX<65 and LocalY>-64 then
object_chunk_north[LocalX][LocalY+64]=1; update_objects("north"); end
-----------------------------------------------------------------------------------southwest
if LocalX > -64 and LocalY > 64 and LocalX <1 and LocalY<129 then
object_chunk_southwest[LocalX+64][LocalY-64]=1; update_objects("southwest"); end
-----------------------------------------------------------------------------------northeast
if LocalX > 64 and LocalY > -64 and LocalX <129 and LocalY<1 then
object_chunk_northeast[LocalX-64][LocalY+64]=1; update_objects("northeast"); end
-----------------------------------------------------------------------------------east
if LocalX > 64 and LocalY < 65 and LocalX <129 and LocalY>0 then
object_chunk_east[LocalX-64][LocalY]=1; update_objects("east"); end
-----------------------------------------------------------------------------------south
if LocalX > 0 and LocalY < 129 and LocalX <65 and LocalY>64 then
object_chunk_south[LocalX][LocalY-64]=1; update_objects("south"); end
-----------------------------------------------------------------------------------southeast
if LocalX > 64 and LocalY < 129 and LocalX <129 and LocalY>64 then
object_chunk_southeast[LocalX-64][LocalY-64]=1; update_objects("southeast"); end
So let's guess that local x and local y belong to northwest, the code is executed but it still checks whether it's in west, center, north etc (which is not possible). After the execution of northwest for example, I want to end the block and NOT continue to execute the other if's.
I know it's possible, just don't know the syntax.
You can't place "break" anywhere in Lua 5.1, this operation is reserved for breaking the loops specifically. So in order to break the execution, you must enclose it in a loop. Alternatively, you can use "goto" statement.
Note that you probably wouldn't gain anything by doing this since in this particular scenario the CPU will know that any of those branches are "highly unlikely" and will try to skip them altogether by pre-choosing the "false" branch, only reverting to executing one if it finds out it was necessary after all.
Also, if this is perofrmance-critical section, this approach wouldn't have been helpful. Instead of eliminating code execution through branching, you should eliminate the branching altogether, and make it utilize mathematical operations in order to process your data properly instead of relying on multitude of hard-coded routines.
As a more general advice on optimization, learn the quirks of software you're working with (LuaJIT, OpenGL, etc.), and learn the quirks of hardware you're working with (x86/ARM-based CPUs, DDR memory, modern GPUs, etc). Without knowing those two, any attempt at optimization is just a shot in the dark.
Last edited by raidho36 on Wed Sep 07, 2016 9:01 am, edited 1 time in total.
As /H/ said, using elseif will ensure that at most one branch will execute. That said, that might also come with its own drawbacks.
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.
Huh, I didn't paid attention to the code snippet, it doesn't apparently uses elseif.
Then again, the only difference in assembly for either scenario is that with elseif it'll jump to after the last statement after executing any of them. Not that it matters because the CPU will try to skip through all of them as if they evaluated to "false", resulting in sequential jumping from last condition to the next without actually stopping by, and then reverting to executing the single one that did evaluated to true.
It's not performance critical, like at all. Just good practice I guess.
Elseif isn't necessary because the values are so, that only 1 branch will execute. I guess I'm splitting hairs too much here.
In hindsight, i should have said that (depending on order and probability), elseifs will minimize comparisons. But as you said, unless it's in a critical part of a program, it shouldn't matter that much.
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.