lua-users home
lua-l archive

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


On 2013-06-06 5:33 PM, "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.
> Each sample comprises of a sample time and an adc value, I have approximately 5000 samples to transfer to Lua.
>  
> As my CPU is orders of magnitude slower than a typical PC, I am pondering what would be the best way of doing this, as regards simplicity, minimum memory usage, and fastest speed.
>  
> I discounted userdata as I only ever need to get data in one direction C --> Lua (and also I don't understand userdata too well)
>  
> So I am looking at doing it via the C lua table api functions. I would have nested tables, but should the inner table be a hash type table or an array type table ?
>  
> An inner Hash table would give me on the Lua side this nice readable type of syntax
>  
> myTime =  mySamples[434].time
> myValue =  mySamples[434].value
>  
>  
> An Array type inner table would give me  (not quite so readable)
>  
> myTime =  mySamples[434][1]
> myValue =  mySamples[434][2]
>  
> 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 ?
>  
>  
>  
>  

Lua does intern strings and reuse them. This not only prevents having duplicate strings in memory, but allows fast string equality tests (only pointers need to be compared).

As for your application, I wonder if stuffing your data into a string (or a C array, with functions to allow Lua to read it, so as to avoid string internalizing overhead) would be faster. Ideally you should benchmark a few methods.