lua-users home
lua-l archive

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



I am writing an app that must test every character in an input stream.
As i understand it, "s1 = string.sub(s,n,n)" must construct a new string
each time it is called.

Yes.

Would "b = string.byte(s,n)" be appreciably faster ?

It's an interesting thought. I feel that stuff like this can usually best be benchmarked. So I wrote a little script to time for-loops containing just the strbyte and strsub calls [yes, I'm still using Lua4].

As it turns out strsub is about 1.4 times slower than strbyte. I expect Lua5 to give you approximately the same results. But you can easily test this yourself.

Obviously, the difference I measured may not matter at all to your particular application. The effect of this type of peep-hole optimisation can vary tremendously, depending on the rest of your system.

(although all my comparisons would then be number, rather than string,
and i believe slower)

It looks like your beliefs might be wrong on my machine [;-)].

As a side-note: if you intend to do *lots* of byte fetching you might want to do something like

	local strbyte = string.byte

before entering your byte-fetching section. It will save you time.

Ashwin.
--
no signature is a signature.