{"id":1789,"date":"2016-08-25T13:05:05","date_gmt":"2016-08-25T13:05:05","guid":{"rendered":"http:\/\/blog.zhukunqian.com\/?p=1789"},"modified":"2016-08-25T13:07:32","modified_gmt":"2016-08-25T13:07:32","slug":"cc-%e4%b8%8e-lua%e4%b9%8b%e5%89%8d%e4%ba%92%e8%b0%83%e7%9a%84%e6%80%a7%e8%83%bd%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/blog.zhukunqian.com\/?p=1789","title":{"rendered":"c\/c++ \u4e0e lua\u4e4b\u524d\u4e92\u8c03\u7684\u6027\u80fd\u6d4b\u8bd5"},"content":{"rendered":"<p>\u5b8c\u6574\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>\u6d4b\u8bd5\u76ee\u6807\u4e3b\u8981\u662f\u770b\u770bc\u4e0elua\u4e4b\u524d\u4e92\u8c03\u7684\u6027\u80fd\u5982\u4f55\u3002<\/p>\n<p>\u6d4b\u8bd5\u7ed3\u8bba\uff1ac\u8c03\u7528lua\u4e0elua\u8c03\u7528c\u57fa\u672c\u53ea\u6709\u4e00\u4e2a\u6570\u91cf\u7ea7\u7684\u6027\u80fd\u635f\u5931\u3002<\/p>\n<p>\u4ecec\u4e2d\u8c03\u7528lua\uff0c\u4f7f\u7528\u7f13\u5b58ref\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u63d0\u534730%\u7684\u6027\u80fd\u3002<\/p>\n<pre class=\"brush: bash; gutter: true\">#include &lt;lua.h&gt;\r\n#include &lt;lauxlib.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;sys\/time.h&gt;\r\n#include &quot;skynet.h&quot;\r\n\r\n\/\/ \u53d6\u5f53\u524d\u6beb\u79d2\u503c\r\nstatic int\r\nltime(lua_State *L) {\r\n\tstruct timeval tv;\r\n\tgettimeofday(&amp;tv,NULL);\r\n\tlua_pushinteger(L,tv.tv_sec*1000+tv.tv_usec\/1000);\r\n\treturn 1;\r\n}\r\n\r\n\/\/ \u4f9blua\u8c03\u7528\uff0c\u5185\u90e8\u65e0\u4e1a\u52a1\u903b\u8f91\r\nstatic int\r\nltest1(lua_State *L) {\r\n        return 0;\r\n}\r\n\r\n\/\/ \u4f9blua\u8c03\u7528, \u5185\u90e8int\u76f8\u52a0\u5e76\u8fd4\u56de\r\nstatic int\r\nltest2(lua_State *L) {\r\n\tint a1=luaL_checkinteger(L,1);\r\n\tint a2=luaL_checkinteger(L,2);\r\n\tlua_pushinteger(L,a1+a2);\r\n\treturn 1;\r\n}\r\n\/\/ \u5185\u90e8\u8c03\u7528lua\u4e2d\u65b9\u6cd5\r\nstatic int\r\nltest3(lua_State *L) {\r\n\tlua_getglobal(L,&quot;global_test_func1&quot;);\r\n\tlua_call(L,0,0);\r\n\treturn 0;\r\n}\r\n\/\/ \u5185\u90e8\u8c03\u7528lua\u4e2d\u65b9\u6cd5\uff0c\u5e76\u53d6\u5f97lua\u4e2d\u8fd4\u56de\u503c\r\nstatic int\r\nltest4(lua_State *L) {\r\n\tlua_getglobal(L,&quot;global_test_func2&quot;);\r\n\tlua_pushinteger(L,1);\r\n\tlua_pushinteger(L,2);\r\n\tlua_call(L,2,1);\r\n\tint sum = (int)lua_tointeger(L, -1); \r\n\tlua_pop(L, 1); \r\n\tlua_pushinteger(L,sum);\r\n\treturn 1;\r\n}\r\n\/\/ \u5185\u90e8\u8c03\u7528lua\u65b9\u6cd510000\u6b21\r\nstatic int\r\nltest5(lua_State *L) {\r\n\tint sum=0;\t\r\n\r\n\tfor(int m=0;m&lt;10000000;m++){\r\n        \tlua_getglobal(L,&quot;global_test_func2&quot;);\r\n        \tlua_pushinteger(L,1);\r\n        \tlua_pushinteger(L,2);\r\n        \tlua_call(L,2,1);\r\n        \tsum=(int)lua_tointeger(L, -1);\r\n\t\tlua_pop(L, 1); \r\n\t}\r\n\r\n        lua_pushinteger(L,sum);\r\n        return 1;\r\n}\r\n\/\/ \u548ctest5\u4e00\u8d77\u5bf9\u6bd4\r\nstatic int\r\nltest6(lua_State *L) {\r\n        for(int m=0;m&lt;10000000;m++){\r\n                lua_getglobal(L,&quot;global_test_func2&quot;);\r\n                lua_pushinteger(L,1);\r\n                lua_pushinteger(L,2);\r\n                lua_call(L,2,1);\r\n                lua_pop(L, 1);\r\n        }\r\n        lua_pushinteger(L,3);\r\n        return 1;\r\n}\r\n\/\/ \u548ctest5\u4e00\u8d77\u505a\u5bf9\u6bd4\r\nstatic int\r\nltest7(lua_State *L) {\r\n\tint func2_handler;\r\n\tlua_getglobal(L,&quot;global_test_func2&quot;);\r\n\tfunc2_handler=luaL_ref(L,LUA_REGISTRYINDEX);\r\n\r\n\tfor(int m=0;m&lt;10000000;m++){\r\n\t\tlua_rawgeti(L,LUA_REGISTRYINDEX,func2_handler);\r\n\t\tlua_pushinteger(L,1);\r\n\t\tlua_pushinteger(L,2);\r\n\t\tlua_call(L,2,1);\r\n\t\tlua_pop(L,1);\r\n\t}\r\n\t\r\n\t\/\/luaL_unref(L,LUA_REGISTRYINDEX);\r\n\tlua_pushinteger(L,3);\r\n\treturn 1;\r\n}\r\n\r\n\/\/ \u548ctest5\u4e00\u8d77\u505a\u5bf9\u6bd4\u4e00\u4e0b\r\nint add(int a,int b){\r\n\treturn a+b;\r\n}\r\nstatic int\r\nltest8(lua_State *L) {\r\n\tfor(int m=0;m&lt;10000000;m++){\r\n\t\tadd(1,2);\r\n\t}\r\n\treturn 0;\r\n}\r\n\r\n\r\n\/\/ full userdata\r\n\r\n\/\/ light userdata\r\n\r\nint\r\nluaopen_perf(lua_State *L) {\r\n        luaL_checkversion(L);\r\n        luaL_Reg l[] = {\r\n\t\t{ &quot;time&quot;,  ltime  },\r\n                { &quot;test1&quot;, ltest1 }, \/\/ \u4ecelua\u4e2d\u76f4\u63a5\u8c03\u7528c\uff0c\u5185\u90e8\u65e0\u903b\u8f91\r\n                { &quot;test2&quot;, ltest2 },\r\n\t\t{ &quot;test3&quot;, ltest3 },\r\n\t\t{ &quot;test4&quot;, ltest4 },\r\n\t\t{ &quot;test5&quot;, ltest5 },\r\n\t\t{ &quot;test6&quot;, ltest6 },\r\n\t\t{ &quot;test7&quot;, ltest7 },\r\n\t\t{ &quot;test8&quot;, ltest8 },\r\n                { NULL, NULL}\r\n        };\r\n        luaL_newlib(L,l);\r\n\r\n        return 1;\r\n}<\/pre>\n<p>lua\u4ee3\u7801:<\/p>\n<pre class=\"brush: bash; gutter: true\">require &quot;common&quot;\r\n\r\nlocal perf = require &quot;perf&quot;\r\n\r\n\r\nfunction global_test_func1()\r\n--\tprint(&quot;global_test_func1&quot;)\r\nend\r\nfunction global_test_func2(a,b)\r\n\treturn a+b\r\nend\r\nlocal t1\r\nlocal t2\r\n\r\nt1=perf.time()\r\nfor i=1,10000000 do\r\n\tperf.test1()\r\nend\r\nt2=perf.time()\r\nprint(&quot;test1:&quot;..(t2-t1))\r\n\r\nt1=perf.time()\r\nfor i=1,10000000 do\r\n\tperf.test2(1,2)\r\nend\r\nt2=perf.time()\r\nprint(&quot;test2:&quot;..(t2-t1))\r\n\r\nt1=perf.time()\r\nfor i=1,10000000 do\r\n\tperf.test3()\r\nend\r\nt2=perf.time()\r\nprint(&quot;test3:&quot;..(t2-t1))\r\n\r\nt1=perf.time()\r\nfor i=1,10000000 do\r\n\tperf.test4()\r\nend\r\nt2=perf.time()\r\nprint(&quot;test4:&quot;..(t2-t1))\r\n\r\nt1=perf.time()\r\nperf.test5()\r\nt2=perf.time()\r\nprint(&quot;test5:&quot;..(t2-t1))\r\n\r\nt1=perf.time()\r\nperf.test6()\r\nt2=perf.time()\r\nprint(&quot;test6:&quot;..(t2-t1))\r\n\r\nt1=perf.time()\r\nperf.test7()\r\nt2=perf.time()\r\nprint(&quot;test7:&quot;..(t2-t1))\r\n\r\nt1=perf.time()\r\nperf.test8()\r\nt2=perf.time()\r\nprint(&quot;test8:&quot;..(t2-t1))\r\n\r\n<\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre class=\"brush: bash; gutter: true\">test1:470\r\ntest2:777\r\ntest3:1619\r\ntest4:1941\r\ntest5:1490\r\ntest6:1437\r\ntest7:675\r\ntest8:40<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b8c\u6574\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a \u6d4b\u8bd5\u76ee\u6807\u4e3b\u8981\u662f\u770b\u770bc\u4e0elua\u4e4b\u524d\u4e92\u8c03\u7684\u6027\u80fd\u5982\u4f55\u3002 \u6d4b\u8bd5\u7ed3\u8bba\uff1a &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/blog.zhukunqian.com\/?p=1789\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/1789"}],"collection":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1789"}],"version-history":[{"count":3,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/1789\/revisions"}],"predecessor-version":[{"id":1792,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/1789\/revisions\/1792"}],"wp:attachment":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}