3
votes

I have a lua code where a certain table (lets say 'abc') has 50,000 key value pairs. Now i want to create a long string which will look like "(key1, value1) (key2,value2) (key3, value3) ... ".

This works for tables with smaller number of elements. for 50,000 it works but the pc becomes very very slow. Is there a way around this ?

I tried inserting in to another table and then concatenating since the string concatenation in lua is expensive but still the performance penalty is there

local pqr = {}
for key, value in pairs(app_revenue) do
  table.insert(pqr, "(")
  table.insert(pqr, key)
  table.insert(pqr, ",")
  table.insert(pqr, value)
  table.insert(pqr, ") ")
end
local x = table.concat(pqr)

Any insight on this issue is much appreciated. Thank you

2
And BTW I found this during my search. May be will help someone in future. (lua.org/pil/11.6.html) - user4700203
What do the key value pairs look like for app_revenue? I did a test with this and it doesn't seem that slow. On my machine it finished in 0.381 seconds. On repl.it it took less than 2 seconds to finish which isn't too bad considering it's interpreted inside a javascript vm. - greatwolf
the key values look like ('application|cricket2015|totalscore|match1', 623 ).. I think there is an issue with the scenario i am working on. (machine and the frameworks i am using) - user4700203

2 Answers

3
votes

You can use a temporary table and string formatting. Look, pretty fast:

local t={}

for i=1,50000 do 
    t[i] = ''..i
end

local temp = {}

for k,v in ipairs(t) do
    temp[#temp+1] = string.format('(%d, %s)', k, v)
end

local x = table.concat(temp,' ')

print(string.sub(x,1,99), #x)

less than a second: http://ideone.com/tSLeBH

1.3 seconds for 500000 entries on my machine

0
votes

Some things to try in your original code. Try each in sequence, measuring the time.

  • Add local table=table before the loop.

  • Add local insert=table.insert before the loop and use insert in the loop.

  • Avoid table.insert: add local n=0 before the loop and use n=n+1; pqr[n]= "(", etc.

  • Avoid repeated insertions: do n=n+1; pqr[n]="("..key..","..value..") " in the loop.

I'd be curious to know which one made a real difference, if any.