lua-users home
lua-l archive

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


2012/1/4 Asim <asim.r.p@gmail.com>:
> I want to write a C function that returns a sequence of bytes.  This
> function should be called from Lua.  There are two options for the type of
> return value in Lua - userdata and string.
>
> The Lua function that will call this C function wants to access individual
> bytes of the returned sequence.  If string is chosen as the type of return
> value from C, then the bytes can be accessed as string.sub(ret_str, i, i)
> where i ranges from 1 to length of ret_str.  string.byte(ret_str, i) is also
> an option.
>
> If userdata is chosen as the type of return value from C, then a metatable
> can be created by the C function before returning the userdata object. The
> metatable should allow indexed access to individual bytes in the userdata
> object (Is it possible to write such a metatable?).  This would allow
> ret_val[i] expression to access individual bytes in Lua.
>
> How to evaluate (and hence compare) the cost of each of the above
> approaches?

First you have to define how to measure the cost (sequence creation
time? data processing time? memory usage?). Then you can implement
both approaches (it's quite easy), write a sample algorithm processing
your byte sequences (something related to your cost definition and end
usage patterns), and benchmark both implementations.