On Sat, Nov 16, 2013 at 9:43 PM, Mike Pall <mike-1311@xxxxxxxxxx> wrote: > Vladimir Dronnikov wrote: > > Need a fast function to convert a FFI char* data to number. > > Well, what kind of number? Integer? Decimal/hex? Floating-point? > Decimal floating-point in general here. > > > src/lj_strscan.c already contains ready-to-use function: > > `StrScanFmt lj_strscan_scan(const uint8_t *p, TValue *o, uint32_t opt)` > > > > Wonder is it exposed to userspace, or how it can be done? > > It's not really exposed, unless you're willing to allocate a > temporary string, i.e. tonumber(ffi.string(cdata)). Arguably that > mostly negates the performance benefit. > Yes, interning char* string leads to circa 700 times slowdown compared to plain Lua tonumber on Lua string, in my tests. > > The easiest solution would be to call strtod() or strtol() via the > FFI. Though this means you're at the mercy of the C library wrt. > accuracy and performance. > ffi.C.atof gives circa 650 times slowdown comared to tonumber on Lua string, ever if I preallocate reusable buffer, sufficient enough to hold copy of source string plus terminating \0. > > If you need to scan a pure integer then a simple multiply-and-add > loop is probably the fastest. But I recommend to find out first what the typical range of numbers will be. Sometimes a little unrolling > can do wonders, e.g. if you mostly scan numbers from 0 to 99. > > I see. Will keep thinking. Thank you. > --Mike > >