lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Follow-up: I moved the assignment to index 0 around a bit.

Assigning to 0 between the first and second loops:

$ cat test.lua
local game_field = {}
local game_size = 1000000
local base_index = tonumber(arg[1])
print("Array indices start with "..base_index)

local function initialize_game_field()
   for j = base_index, base_index + game_size - 1 do
      game_field[j] = "white"
   end
   game_field[0] = "green"
   for j = base_index + game_size, base_index + 2*game_size - 1 do
      game_field[j] = nil
   end
   for j = base_index + 2*game_size, base_index + 3*game_size - 1 do
      game_field[j] = "black"
   end
end

initialize_game_field()
$ time lua test.lua 1
Array indices start with 1

real 7m29.923s
user 7m29.831s
sys 0m0.072s

OK, no significant change from my previous results. What if I move that line down three lines, between the second and third loop? Surely that won't change either, right?

$ cat test.lua
local game_field = {}
local game_size = 1000000
local base_index = tonumber(arg[1])
print("Array indices start with "..base_index)

local function initialize_game_field()
   for j = base_index, base_index + game_size - 1 do
      game_field[j] = "white"
   end
   for j = base_index + game_size, base_index + 2*game_size - 1 do
      game_field[j] = nil
   end
   game_field[0] = "green"
   for j = base_index + 2*game_size, base_index + 3*game_size - 1 do
      game_field[j] = "black"
   end
end

initialize_game_field()
$ time lua test.lua 1
Array indices start with 1

real 0m0.147s
user 0m0.123s
sys 0m0.020s

Uh... wow. That's statistically equivalent to the first run without assigning to index 0 at all.

I don't have the time today to investigate further, but there's some info to get you all started.

On Mon, Nov 23, 2020 at 10:50 AM Jonathan Goble <jcgoble3@gmail.com> wrote:
Can confirm this issue exists in Lua 5.3.5 as well (test.lua is the same as program.lua above):

$ time lua test.lua 1
Array indices start with 1

real 0m0.145s
user 0m0.129s
sys 0m0.012s
$ time lua test.lua 0
Array indices start with 0

real 7m22.883s
user 7m22.836s
sys 0m0.025s

On Mon, Nov 23, 2020 at 10:20 AM Oliver Kroth <oliver.kroth@nec-i.de> wrote:
Try adding

       game_field[0] ="green"

before

initialize_game_field()

and call your script with argument 1...

With that change to the above program, I get (using the same Lua on the same computer):

$ time lua test.lua 1
Array indices start with 1

real 7m38.496s
user 7m38.243s
sys 0m0.076s

Definitely seems to be an issue with assigning to index 0. 3,000 times slower is highly unexpected and should be considered a bug IMO.