lua-users home
lua-l archive

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


> When getting null-terminated strings from Lua, does lua_strlen() offer
any
> speed advantage over regular strlen() calls?

> I imagine Lua stores string lengths in its objects, but am not sure if
going
> through Lua's internal structures will really eat less cpu cycles than
doing a strlen().

> Does anyone here know that for sure?

I guess it depends how long your strings are and what C compiler you are
using. Call lua_strlen() costs you a function call, a
stack-index-to-address conversoin, a couple of sanity checks, and a field
lookup -- perhaps 20 instructions at the outside. If you use a
highly-optimising C compiler which open codes strlen(), and your strings
are all just a few characters long, then strlen() might be a couple of
instructions less. If, on the other hand, strlen() itself does a subroutine
call, it is very unlikely that it will be faster. (And with lua_strlen()
you don't have to worry about whether the string has internal NULs).

On the other hand, if you are importing a string into Lua, you should be
aware that strlen() will be called unless you explicitly specify a length.
So there is certainly no value in exporting a string from C into Lua in
order to call lua_strlen().

Hope that helps.

Rici