[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Performance problem with 0-based arrays
- From: Sean Conner <sean@...>
- Date: Mon, 23 Nov 2020 16:50:42 -0500
It was thus said that the Great Egor Skriptunoff once stated:
>
> This loop is not pointless.
> It clears the cells which may contain "black" or "white" pieces from the
> previous game.
> The function initialize_game_field() is invoked on every game start.
Given that any undefined key in a table returns nil, what's wrong with
this version of initialize_game_field()?
local function initialize_game_field()
game_field = {} -- AN EMPTY TABLE
for j = base_index, base_index + game_size - 1 do
game_field[j] = "white"
end
for j = base_index + 2*game_size, base_index + 3*game_size - 1 do
game_field[j] = "black"
end
end
Not only does the 0 case run as fast as the 1 case, but it runs a bit
faster overall as there's only two loops doing work, not three.
> Yes, it's the same bug.
> Your previous opinion was:
> "It does not occur often in real code, so we can treat it as a rare
> worst-case situation and don't fix"
> But your explanation is not pertinent to this new bugreport.
> We fill the array consequently from min index to max index.
> It's the most common situation.
> Everyone is doing exactly this way when initializing an array.
> Can you say now: "It does not occur often in real code"?
I can see it being a problem for someone new to Lua (or to programming),
but I would wonder how often people reuse tables in Lua in this way.
-spc