lua-users home
lua-l archive

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


>>> roberto@inf.puc-rio.br 01/29/03 03:07 AM >>>
.. snipped real message, left comment...

Frequently the problem is not performance, but the perception people
have about performance.


Oh yeah. One thing I haven't seen is any real numbers on the overhead of
doing lookups. Here are a few that might help..

A little timing test I like to do to get a rough idea about the speed of
Lua on a machine is this one:

    function fact(n)
        if n<=1 then 
          return 1 
        else 
          return n*fact(n-1)
        end
    end
    do local t=os.clock()
       for i=1,1000000 do fact(10) end
       print(os.clock()-t)
    end

On my iBook 500MHz OSX 10.2 I get 8.48 seconds

If we modify this so factorial is done this way:

x = {}
function x:fact(n)
    if x<=1 then
      return 1
    else
      return n*self:fact(n-1)
    end
end

I modified the timing code to use x:fact(10) and the time was slightly
FASTER at 8.44 seconds.

Now make x into a metaclass and use the __index as a table trick:

y = {}
x.__index = x
setmetatable(y,x)

Change the timing code to use y:fact(10) and I get 11.77 seconds.
Definitely a slowdown, but not too bad.

I'd like to send you some timing for making __index a function, but I
have a class to teach in 4 minutes.

  - Tom