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.