2010/5/4 steve donovan
<steve.j.donovan@gmail.com>
On Tue, May 4, 2010 at 1:00 PM, Paul Hudson <
phudson@pobox.com> wrote:
> examples. Is improving this step actually something that would make a
> noticeable difference?
Well, let's expose lua_creatable:
[[
// xtable.c
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
static int l_createtable (lua_State *L) {
int narr = luaL_optint(L,1,0); // initial array slots, default 0
int nrec = luaL_optint(L,2,0); // intial array of hash slots, default 0
lua_createtable(L,narr,nrec);
return 1;
}
static const luaL_reg xtable[] = {
{"create",l_createtable},
{NULL,NULL}
};
LUALIB_API int luaopen_xtable(lua_State *L)
{
luaL_register (L, "xtable", xtable);
return 1;
}
]]
The resulting test:
[[
require 'xtable'
local create = xtable.create
for i = 1, 10000000 do
local t = create(0,5) -- 5 hash slots
t.a = i; t.b = i; t.c = i; t.d = i; t.e = i
end
]]
takes 8.9 sec
whereas
[[
for i = 1, 10000000 do
local t = {}
t.a = i; t.b = i; t.c = i; t.d = i; t.e = i
end
]]
takes 18.2 sec
Whether this would actually make a serious difference in a real
application, I don't know.
But the point is that we don't need to add to the public API to get
this functionality exposed.
The code used for measurement doesn't reflect object oriented code.
In the case of object oriented, the size of array/hash part is not really known,
but an existing table can used as template.
François
steve d,