lua-users home
lua-l archive

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



On 20-Sep-06, at 9:37 AM, Roberto Ierusalimschy wrote:

(The former was the answer to a question on IRC the other day: how do I
turn a
table which looks like {1, 2, 0, 9, 7, 5} into the string "120975".)

What about table.concat(t) ?

It's slower :)

rlake@freeb:~$ time lua511 -e 'for i = 1, 1e5 do local t = {1,2,3,4,5,6,7}; local a = ("%d"):rep(#t):format(unpack(t)) end'
real    0m0.646s
user    0m0.635s
sys     0m0.010s

rlake@freeb:~$ time lua511 -e 'for i = 1, 1e5 do local t = {1,2,3,4,5,6,7}; local a = table.concat(t) end'
real    0m0.876s
user    0m0.863s
sys     0m0.009s

OK, so I could have speeded that up a bit by making table.concat a local:

rlake@freeb:~$ time lua511 -e 'local tconcat = table.concat; for i = 1, 1e5 do local t = {1,2,3,4,5,6,7}; local a = tconcat(t) end'
real    0m0.846s
user    0m0.844s
sys     0m0.002s

But mine is still 25% faster, and can be improved even more by memoising:

rlake@freeb:~$ time lua511 -l 'Memoise' -e 'local rep = Memoise(function(n) return ("%d"):rep(n) end); for i = 1, 1e5 do local t = {1,2,3,4,5,6,7}; local a = rep[#t]:format(unpack(t)) end'
real    0m0.569s
user    0m0.558s
sys     0m0.010s