lua-users home
lua-l archive

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


Of course, as usual, the answer is, it depends.  In your specific example you
are, more or less, inlining the functions in the if/else section, so there is
going to be some performance gains there. If, in the actual code, you are
going to be calling the same functions, the difference will be less. 

There could be the argument made that, if you knew that some options were MUCH
more likely to happen the if/else code would have a better amortized cost.  
Your best bet is probably to check out some of the lua profiler options and
try it out.  If you are on the windows platform, you may want to look into
LuaJIT, which may change the relative performance.

Another thing to keep in mind is that if you are using the object oriented
approach, and calling the table functions like:
actions:pAuthRequest()
the actions parameter is passed (as self) very cheaply (compared to calling
actions.pAuthRequest(actions) ) so if you need to keep state with these
functions it may make sense to attach both the functions and any needed state 
to that table.

In the end though (without doing actual benchmarks) the performance difference
may not be enough to outweigh the maintenance costs.

function handleAction(action)
  return actions[action]()
end

function actions.pAuthRequest()
  print("Auth request coming!")
end

function actions.ClientSend()
  master:send(auth_string)
end

...

is much more maintainable (in my opinion) than

function handleAction(action)
  if(action=='pAuthRequest') then
    print("Auth request coming!")
  elseif(action =='ClientSend') then
    master:send(auth_string)
  elseif ...
  end
end