[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: performance of objects with full privacy
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Tue, 15 Apr 2008 16:23:04 -0400
mr wrote:
> function privacy1(init)
> local data = init.data
> return function(m, arg)
> if m == "getdata" then return data end
> if m == "setdata" then data = arg end
> end
> end
>
> function privacy2(init)
> local data = init.data
> local function getdata() return data end
> local function setdata(arg) data = arg end
> return function(m)
> return nil
> or ("getdata" == m and getdata)
> or ("setdata" == m and setdata)
> end
> end
>
> function privacy3(init)
> local data, public = init.data, {}
> function public.getdata() return data end
> function public.setdata(arg) data = arg end
> return function(m)
> return public[m]
> end
> end
>
> function privacy4(init)
> local data, public = init.data, {}
> function public.getdata() return data end
> function public.setdata(arg) data = arg end
> return setmetatable({}, {__index = function(t,k)
> return public[k] end, __newindex = function() error()
> end})
> end
>
> Which of these approaches has the best performance for
> objects with 20 - 30 methods?
Write a benchmark on your system (OS, hardware, Lua version, dataset),
and figure out by yourself, you will get more accurate and reliable
data. Do you want help to write such a benchmark ?
> How big must N be that one table lookup is faster then
> executing N if-then blocks?
It's very dependent on the probability distribution of the values you
want to match. If one key (eg. "getdata") is looked for 99% of the time,
and your first if test for that key, it's likely to be faster than a
table lookup, no matter how many 'if' there is after that one.