lua-users home
lua-l archive

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



On Jul 24, 2004, at 09:55, Adrian Sietsma wrote:

I am looking at cutting an existing c++ algorithm to lua.
The current c++ version uses a lot (~30) of #defined state flags.

my lua options seem to be
1/ just use integer constants
if s == 1 then ...... elseif s==2 then ... end
should be fastest, but hardest to read.

2/ use local vars
local STATE_1, STATE_2, STATE_3 = 1,2,3
if s == STATE_1 then ... end

this reads nicely, but all those locals are a pain, and i don't really need variables - the states are just symbols

3/ use strings
if s == "STATE_1" then ...... end

still reads nicely, but are all those string comparisons going to cost much ?

String equality (s == "STATE_1") is very fast as strings are hashed (interned) and this test is just a pointer comparison. I'd go with strings.


any other suggestions ?

i don't like the #define approach, but could lua do with a "const" keyword, for immutable data (symbolic constants)? this would presumably be translated to the literal value during compile.
eg
const STATE_1 = 1
if (s == STATE_1) then...

would compile to
if (s == 1) then...

feel free to tell me that is impossible / a bad idea because.. / doesn't match the LUA design philosophy / etc.

It comes up a lot on the list, you search the archives if you feel like discussing it.

Cheers,
 drj