[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Fastest way to transfer data from C to Lua ?
- From: Coda Highland <chighland@...>
- Date: Thu, 6 Jun 2013 14:55:08 -0700
On Thu, Jun 6, 2013 at 2:32 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 ?
>
This is one of the classic examples of where LuaJIT shines -- in
LuaJIT you can actually use a pointer to an array of C structures and
get C-like performance out of it. Yes, you're penalized by having a
C->Lua call in there, but after the handoff you're golden.
/s/ Adam