[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re[2]: Suggestion: add a new api lua_pushuserdata
- From: Cloud Wu <cloudwu@...>
- Date: Thu, 14 Dec 2006 03:55:45 +0800
Hello Roberto,
Wednesday, December 13, 2006, 7:17:17 PM, you wrote:
RI> By "expensive" you mean too slow? If you are going to call back Lua,
RI> it seems that two extra table accesses should make no relevant difference.
I think sometimes it seems slow, if I only call back lua for a simple operation. (IMHO)
I wrote a small program to test :
The code in C is:
-------------test.c -------------------
static void *p[1024];
static int a=0;
static int
test(lua_State *L)
{
lua_pushlightuserdata(L,p);
lua_rawget(L,LUA_REGISTRYINDEX);
lua_pushlightuserdata(L,p[a]);
lua_rawget(L,-2);
a=(a+1)%1024;
return 1;
}
EXPORT int
luaopen_test(lua_State *L)
{
int i;
lua_pushlightuserdata(L,p);
lua_createtable(L,0,1024);
for (i=0;i<1024;i++) {
p[i]=lua_newuserdata(L,4);
lua_pushlightuserdata(L,p[i]);
lua_insert(L,-2);
lua_rawset(L,-3);
}
lua_settable(L,LUA_REGISTRYINDEX);
lua_pushcfunction(L,test);
return 1;
}
------------------------------------
In lua, I use os.clock() to timing:
------------------------------------
local test=require"test"
local function dummy()
end
function timing(f)
local t=os.clock()
for i=1,1024*1024 do
f()
end
print(os.clock()-t)
end
timing(test)
timing(dummy)
----------------------------------
In my PC, the result is
0.219
0.094
test() cost more time than dummy()
--
Best regards,
Cloud mailto:cloudwu@163.com
http://blog.codingnow.com
[学问二字, 须要拆开看, 学是学, 问是问]