lua-users home
lua-l archive

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


On 7/06/2013, at 9:32 AM, Geoff Smith <spammealot1@live.co.uk> wrote:

> Hi
>  
> I have an application where I have generated a bunch of ADC samples from a chip that I need to transfer over to Lua.
> ... 
> The second method sounds like it would be faster, and less memory to store on the Lua side. But I wouldn't be surprised if Lua did some tricks for the hash table example where it optimises its key string storage so it doesn't need to store the key strings "time" and "value" 5000 times. So maybe it would be of similar memory cost, and maybe only slightly slower ?
>  
> Would be interesting if someone more knowlegable could speculate on how they compare ?

I'm not more knowledgeable, and I won't speculate, but here's a rough test of something that is not the same as what you plan to do.  Your time to fill the array will be different because you're doing it from the API, not in Lua, but this might give an indication?

$ lua test.lua
Hash: 0.690967 s, 153.327474 MB
Array: 0.621593 s, 107.552795 MB


test.lua:

local start = os.clock()
local mem = collectgarbage("count")

local data = {}
for i = 1, 1000000 do
  data[i] = { time = math.random(), value = math.random() }
end

collectgarbage("collect")
io.stdout:write(("Hash: %f s, %f MB\n"):format(os.clock() - start, (collectgarbage("count") - mem) / 1024))

data = nil
collectgarbage("collect")
local start = os.clock()
local mem = collectgarbage("count")

data = {}
for i = 1, 1000000 do
  data[i] = { math.random(), math.random() }
end

collectgarbage("collect")
io.stdout:write(("Array: %f s, %f MB\n"):format(os.clock() - start, (collectgarbage("count") - mem) / 1024))