What's faster: table or string?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Kasperelo
Party member
Posts: 343
Joined: Fri Apr 13, 2012 1:47 pm
Location: The Milky Way

What's faster: table or string?

Post 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:

Code: Select all

table = {123, 456}
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?
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: What's faster: table or string?

Post 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
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
Kasperelo
Party member
Posts: 343
Joined: Fri Apr 13, 2012 1:47 pm
Location: The Milky Way

Re: What's faster: table or string?

Post by Kasperelo »

Ok, that's what I thought :)
Thanks for the detailed reply!
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: What's faster: table or string?

Post by s-ol »

Stick with tables, if you need to optimize later I would suggest using a struct with luajit's ffi.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: What's faster: table or string?

Post 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.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests