lua-users home
lua-l archive

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


Bogdan Marinescu wrote:
> I ran 'life.lua', and the results surprised me. Specifically, I can see lots
> and lots of realloc calls with a one byte difference between the old block
> size and the new block size, in both directions (+1 or -1 difference).

Replace _CELLS:draw() with:

   function _CELLS:draw()
     local out={} -- accumulate to reduce flicker
     for y=1,self.h do
      for x=1,self.w do
         out[#out + 1] = self[y][x] > 0 and ALIVE or DEAD
       end
       out[#out + 1] = '\n'
     end
     write(table.concat(out))
   end


Mike Pall wrote:
>  This is not a recommended practice and hopefully not representative for
>  professionally written Lua programs.

When Lua is used as an end-user scripting language, a lot of Lua code
is *not* written by professionals. There are hundreds of World of
Warcraft addons with code just as bad as that loop in life.lua. *sigh*

Cheers,
Eric