lua-users home
lua-l archive

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

>   s=""
>   for i=1,100000 do
>     s=s.."x"
>   end

Are you sure this is from "life.lua"? I didn't find it there...

Anyway, that code can really gives an "out of memory" error. This is not
a bug, but a limitation of the way Lua manipulates strings. (Java has
similar problems.)

Everytime you do `s = s.."x"' you create a new string. So, when s has
size 50,000, after that statement you have two strings, the original one
with 50,000 elements and a new one with 50,001 elements. After another
loop you have three strings, and so on. Even if you do not run out of
memory, you will use huge amounts of memory and of CPU time.

See for a long discution about
how to do this "loop" efficiently (if you really need it).

See also for a
discussion about String Concatenation in several languages (and another
example on how to do it efficiently in Lua).

-- Roberto