Created
December 9, 2012 01:56
-
-
Save AndrewTsao/4242951 to your computer and use it in GitHub Desktop.
测试Lua table中以lightuserdata和string作key的性能
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| all: | |
| gcc -O2 -fpic -c -o symbol.o symbol.c | |
| gcc -O -shared -fpic -o symbol.so symbol.o |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <lua.h> | |
| #include <lauxlib.h> | |
| static const char *l_sym_click = "click"; | |
| static int l_open(lua_State *L) { | |
| void *h = (void **)lua_newuserdata(L, sizeof(void *)); | |
| lua_newtable(L); | |
| lua_setfenv(L, -2); | |
| return 1; | |
| } | |
| static int l_regsym(lua_State *L) { | |
| lua_getfenv(L, 1); | |
| lua_pushvalue(L, 2); | |
| lua_pushvalue(L, 3); | |
| lua_rawset(L, -3); | |
| return 1; | |
| } | |
| static int l_checksym(lua_State *L) { | |
| luaL_checktype(L, 1, LUA_TUSERDATA); | |
| lua_getfenv(L, 1); | |
| lua_pushlightuserdata(L, l_sym_click); | |
| lua_rawget(L, -2); | |
| return 1; | |
| } | |
| static int l_checkstr(lua_State *L) { | |
| luaL_checktype(L, 1, LUA_TUSERDATA); | |
| lua_getfenv(L, 1); | |
| lua_pushstring(L, "click"); | |
| lua_rawget(L, -2); | |
| return 1; | |
| } | |
| static luaL_Reg sym_lib[] = { | |
| {"handle", l_open}, | |
| {"regsym", l_regsym}, | |
| {"checksym", l_checksym}, | |
| {"checkstr", l_checkstr}, | |
| {NULL, NULL}, | |
| }; | |
| LUALIB_API int luaopen_symbol(lua_State *L) { | |
| luaL_openlib(L, "symbol", sym_lib, 0, 0); | |
| lua_pushlightuserdata(L, l_sym_click); | |
| lua_setfield(L, -2, l_sym_click); | |
| return 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require"symbol" | |
| os = require"os"; | |
| local clock = os.clock | |
| local h = symbol.handle() | |
| function cb() end | |
| local s = os.clock() | |
| local click = symbol.click | |
| symbol.regsym(h, click, cb) | |
| for i=0, 1e7 do | |
| symbol.checksym(h) | |
| end | |
| print(clock() - s) | |
| local s = clock() | |
| local click = 'click' | |
| symbol.regsym(h, click, cb) | |
| for i=0, 1e7 do | |
| symbol.checkstr(h) | |
| end | |
| print(clock() - s) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
用字符串作key, 用lightuserdata (指针)作key 1e7次设置与check分别是
1.05
1.2