Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bad things about your favourite programming language
#17
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.
Reply


Messages In This Thread
Bad things about your favourite programming language - by Russt - 2010-09-01, 09:40 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)