lua-users home
lua-l archive

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


Posti Laatikko wrote:
> I like to clarify what i mean was overhead using variables & functions
> using metatable __index OOP method.

Metatable chaining has some overhead. But it's hard to answer
your question without knowing the expected usage patterns
(creations vs. method calls vs. chained method calls).

Your best bet is to do a lot of mini-benchmarks. That's what
I do whenever I can't guess which approach is best. And in fact
my guesses have been proved wrong by benchmarks often enough.

Try something like this (lines split for readability):


  time lua -e 'local Account = require"Account"; \
  for i=1,1e5 do local o = Account:new() end'

  time lua -e 'local Account = require"Account"; \
  for i=1,1e5 do local o = Account:new(); for j=1,10 do o:method() end end'

And so on. Try to model the expected usage patterns and time
the result with different variants of your library. You have
to tune the outer loop so this benchmark runs long enough to
show a timing difference. A few seconds of run time are good
enough to answer simple questions.

> Also like to know if using LuaJIT overhead between these two methods
> lowers?

The general rule is: performance relationships between different
programming methods observed under Lua hold under LuaJIT, too
(except with different factors). E.g. locals are a lot faster than
globals and this is still true for LuaJIT.

There are a few minor exceptions to this rule, but they might
change in future versions of LuaJIT, so don't depend on any
of them (if you find them ;-)).

> Because like to use best possible method performance wise in
> my whole code because i'll be using objects alot.

You may not like these answers:

1. Non-OOP approaches (closures et al.) are usually faster than
   the equivalent OOP approach. No, this does not mean you have
   to give up structured and modular programming. Sorry, I don't
   know of any introductory references in the context of Lua.

2. "Premature optimization is the root of all evil." (Donald Knuth)
   I.e. program first, benchmark later and only then start to
   optimize things. Only if you have to make some 'big' decisions
   it's useful to do some benchmarks experiments before you start.

3. Expect to refactor/rewrite your program a few times if you
   want it to perform really good. Most programmers or companies
   don't have the guts or the insight to promote this.

[I've completely rewritten the LuaJIT code generation engine
three times. I've changed the JIT->JIT calling conventions four
times (with all the ensuing rewrites). And I already know I have
to throw away the whole optimizer in one of the next versions ...]