Skip to content

Instantly share code, notes, and snippets.

@xjdrew
Created August 14, 2020 03:15
Show Gist options
  • Save xjdrew/03203bfab33664daabe0ddce1af3df7e to your computer and use it in GitHub Desktop.
Save xjdrew/03203bfab33664daabe0ddce1af3df7e to your computer and use it in GitHub Desktop.

Revisions

  1. xjdrew created this gist Aug 14, 2020.
    62 changes: 62 additions & 0 deletions map.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    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;
    }
    }
    4 changes: 4 additions & 0 deletions result.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    lmap consume: 2.633608
    cmap consume: 2.532202
    lmaps consume: 12.284357
    cmaps consume: 9.247168
    1 change: 1 addition & 0 deletions scripts.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    g++ -O3 -fPIC -shared -o map.so map.cpp & lua table_perf.lua
    63 changes: 63 additions & 0 deletions table_perf.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    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))