lua-users home
lua-l archive

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




Petri Häkkinen <petrih3@gmail.com>于2020年11月25日 周三03:30写道:

On 24. Nov 2020, at 18.59, Egor Skriptunoff <egor.skriptunoff@gmail.com> wrote:

On Tue, Nov 24, 2020 at 6:46 PM Norman Ramsey wrote:
why wouldn't you simply allocate a fresh table?
Why try to reuse an old one?

The reusing is significantly faster:

local game_field = {}
local game_size = 1000000

local function reuse_old()
   for j = 1, game_size do
      game_field[j] = "white"
   end
   for j = 1 + game_size, 2*game_size do
      game_field[j] = nil
   end
   for j = 1 + 2*game_size, 3*game_size do
      game_field[j] = "black"
   end
end

local function create_new()
   game_field = {}
   for j = 1, game_size do
      game_field[j] = "white"
   end
   for j = 1 + 2*game_size, 3*game_size do
      game_field[j] = "black"
   end
end

local t = os.clock()
for i = 1, 100 do
   reuse_old()
end
print(os.clock() - t)   --> 6.874127

local t = os.clock()
for i = 1, 100 do
   create_new()
end
print(os.clock() - t)   --> 36.050187


Perhaps it’s again time to consider adding table.new? Rehashing is heavy when initializing these kind of large tables...


Our server was a complete static memory C++ ones for hold many players online (15000 players one process), so we really need table reuse, and the best API we most desired is the lua_resettable()...--
regards,
Xavier Wang.