2010-09-01, 09:40 PM
Spaz Wrote:C# and Java have StringBuilder classes that allow you to build up a string like in that loop without incurring the cost of creating a new string object with each concatenation. Maybe Lua has something like that?
Well yeah the proper way to do it is insert all the little strings into a table and call concat() to collapse them all into one, like so:
Code:
t = {}
for i = 1, 100000 do
table.insert(t, 'a')
end
s = table.concat(t)which executes almost instantly.
It's just that the naive method has ridiculous costs, that's not to say there isn't a better method, and it isn't the most straightforward one.

