Created
August 14, 2020 03:15
-
-
Save xjdrew/03203bfab33664daabe0ddce1af3df7e to your computer and use it in GitHub Desktop.
hash_map vs lua table
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
extern "C" { | |
#include "lua.h" | |
#include "lauxlib.h" | |
} | |
#include <string> | |
#include <ext/hash_map> | |
using namespace std; | |
using namespace __gnu_cxx; | |
namespace __gnu_cxx { | |
template<> struct hash<string> | |
{ | |
size_t operator()(const string& s) const | |
{ | |
return __stl_hash_string(s.c_str()); | |
} | |
}; | |
} | |
hash_map<int, int> _m; | |
hash_map<string, string> _ms; | |
extern "C"{ | |
static int _set(lua_State *L) { | |
lua_Integer k = luaL_checkinteger(L, 1); | |
lua_Integer v = luaL_checkinteger(L, 2); | |
_m[(int)k] = (int)v; | |
return 0; | |
} | |
static int _get(lua_State *L) { | |
lua_Integer k = luaL_checkinteger(L, 1); | |
lua_pushinteger(L, (int)_m[(int)k]); | |
return 1; | |
} | |
static int _sets(lua_State *L) { | |
const char* k = luaL_checkstring(L, 1); | |
const char* v = luaL_checkstring(L, 2); | |
_ms[string(k)] = string(v); | |
return 0; | |
} | |
static int _gets(lua_State *L) { | |
const char* k = luaL_checkstring(L, 1); | |
lua_pushstring(L, _ms[string(k)].c_str()); | |
return 1; | |
} | |
LUAMOD_API int luaopen_map(lua_State *L) { | |
luaL_Reg l[] = { | |
{ "get", _get}, | |
{ "set", _set}, | |
{ "gets", _gets}, | |
{ "sets", _sets}, | |
{ NULL, NULL }, | |
}; | |
luaL_newlib(L,l); | |
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
lmap consume: 2.633608 | |
cmap consume: 2.532202 | |
lmaps consume: 12.284357 | |
cmaps consume: 9.247168 |
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
g++ -O3 -fPIC -shared -o map.so map.cpp & lua table_perf.lua |
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
local count = 10000000 | |
local lmap = {} | |
local cmap = require "map" | |
local lmap = {} | |
local lmaps = {} | |
local function check_cmap() | |
for i=1, count do | |
cmap.set(i*137,i) | |
end | |
for i=1, count do | |
if cmap.get(i*137) ~= i then | |
error(i) | |
end | |
end | |
end | |
local function check_lmap() | |
for i=1, count do | |
lmap[i*137] = i | |
end | |
for i=1, count do | |
if lmap[i*137] ~= i then | |
error(i) | |
end | |
end | |
end | |
local function check_cmaps() | |
for i=1, count do | |
local v = tostring(i) | |
cmap.sets(v, v) | |
if cmap.gets(v) ~= v then | |
error(i) | |
end | |
end | |
end | |
local function check_lmaps() | |
for i=1, count do | |
local v = tostring(i) | |
lmaps[v] = v | |
if lmaps[v] ~= v then | |
error(i) | |
end | |
end | |
end | |
local function bench(f) | |
local t0 = os.clock() | |
f() | |
local t1 = os.clock() | |
return t1 - t0 | |
end | |
print("lmap consume:", bench(check_lmap)) | |
print("cmap consume:", bench(check_cmap)) | |
print("lmaps consume:", bench(check_lmaps)) | |
print("cmaps consume:", bench(check_cmaps)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment