[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [PROPOSAL] a new function: table.proto()
- From: HyperHacker <hyperhacker@...>
- Date: Tue, 4 May 2010 02:46:14 -0600
You could easily write a C function in your host program, and set
table.proto to it, without having to modify the official code. (That
host program may be just one that sets some variables before calling
dofile(), if you're not using C in your program already.)
On Tue, May 4, 2010 at 02:30, François Perrad <francois.perrad@gadz.org> wrote:
> The goal is to improve the performance of most of the object oriented
> implementations (prototype or class based).
> The idea is to prevent rehashing during the object instanciation.
> It will done by a new function written in C, in the Table library.
> A Lua version could be written :
>
> function table.proto (t)
> local tp = {}
> for k in pairs(t) do
> tp[k] = nil
> end
> return tp
> end
>
> This function creates only the keys in the new table.
> It is not a DeepCopy, ShallowCopy, clone function, ...
> because the behaviour of this kind of function depends
> on the object model.
>
> A first implementation with Lua API was too slower.
> So, I write a second implementation with low level functions.
>
> The following code :
> local p = { a = true, b = true, c = true, d = true, e = true }
> local proto = table.proto
> for i = 1, 1000000 do
> local t = proto(p)
> t.a = i; t.b = i; t.c = i; t.d = i; t.e = i
> end
>
> is 2x faster than :
> for i = 1, 1000000 do
> local t = {}
> t.a = i; t.b = i; t.c = i; t.d = i; t.e = i
> end
>
> and ~10% slower than :
> for i = 1, 1000000 do
> local t = { a = i, b = i, c = i, d = i, e = i }
> end
>
> François Perrad
>
>
--
Sent from my toaster.