Page 1 of 1
What's faster: table or string?
Posted: Wed Aug 12, 2015 11:19 pm
by Kasperelo
I was unsure where to post this, maybe it should go in Support and Development, I don't know. But anyway, what would be faster in Lua:
Making a table and storing two values in it:
or
making a string and cutting out the wanted parts:
Code: Select all
string = "123456"
firstNumber = tonumber(string:sub(1,3))
secondNumber = tonumber(string:sub(4, 6))
and would this difference change based on how many values (or variables) you are storing?
Re: What's faster: table or string?
Posted: Thu Aug 13, 2015 12:19 am
by Positive07
Table is way faster, because you are just doing an atomic operation when creating, one when indexing and one when adding a new item
Compare this to one atomic operation when creating a string, the expensive concat method when adding an item (which is slower than creating a new item) and the tonumber + string.sub functions which are way slower than just indexing a table
tables may take more memory (it also depends on the structure thought, arrays use less memory than hashes) than strings, but you may not even notice the change really
Saving numbers as strings takes more memory than as numbers, for example 126 is 1 byte when stores as a number and 3 bytes when stored as a string
Anyway, optimization is the root of all evil, I recommend you go with the simpler route (tables, which will give you less headaches than strings), if it gets TOO SLOW or consumes TOO MUCH MEMORY, you could try switching to strings, but I dont think that will help
Memory consumption in games is mostly due to graphics, sounds and assets in general more than data (except in 3D when you have thousands and thousands of vertex and vertex data but that is not the case)
Your game gets slower when you call THOUSANDS of functions per update, but you will not get there that easily and even then the most expensive calls are the ones that draw stuff to the screen, not the basic tonumber or string functions
Re: What's faster: table or string?
Posted: Thu Aug 13, 2015 9:13 pm
by Kasperelo
Ok, that's what I thought
Thanks for the detailed reply!
Re: What's faster: table or string?
Posted: Thu Aug 13, 2015 11:49 pm
by s-ol
Stick with tables, if you need to optimize later I would suggest using a struct with luajit's ffi.
Re: What's faster: table or string?
Posted: Fri Aug 14, 2015 10:23 am
by Robin
Use whatever makes sense (so tables in this case), and it'll probably be faster as a bonus.
Positive07 wrote:Saving numbers as strings takes more memory than as numbers, for example 126 is 1 byte when stores as a number and 3 bytes when stored as a string
Okay, but that is wrong. How numbers are stored depends on the Lua build, but generally, all numbers are stored as double precision floats, so every number, whether it is 3000032030303303, 0.00000001 or 126 takes 8 bytes to store. (This may be different for LuaJIT, but it may not, and I wouldn't depend on it), "126" might still use more memory, because it also needs to store the length of the string somewhere.