C呼叫lua指令碼的效率測試

2021-08-23 13:10:56 字數 1382 閱讀 2028

c呼叫lua指令碼的效率測試

以下**以c語言為基準,測試了c呼叫lua迴圈和迴圈呼叫lua的效率。結論是不要頻繁地穿越c/lua邊界.

#include

extern "c"

/* lua直譯器指標 */

const char lua_script =

"function loop_add(a, b) "

" local sum = 0 "

" for i = 1, 10000000 do "

" sum = sum + a + b "

" end "

" return sum "

"end "

" ""function add(a, b) "

" return a + b "

"end "

;// lua 指令碼裡面的函式由c呼叫

int use_lua_add(lua_state *l, const char *func_name, int x, int y)

int main()

sum = 0;

tstart = clock();

for (i = 0; i < 10000000; i++)

tstop = clock();

printf("c++: %dms.\nthe sum is %u.\n",

(tstop - tstart) * 1000 / clocks_per_sec, sum);

sum = 0;

tstart = clock();

sum = use_lua_add(l, "loop_add", 1, 1);

tstop = clock();

printf("lua loop_add: %dms.\nthe sum is %u.\n",

(tstop - tstart) * 1000 / clocks_per_sec, sum);

sum = 0;

tstart = clock();

for (i = 0; i < 10000000; i++)

tstop = clock();

printf("loop lua add: %dms.\nthe sum is %u.\n",

(tstop - tstart) * 1000 / clocks_per_sec, sum);

lua_close(l);

return 0;}

執行結果:

c++: 31ms.

the sum is 20000000.

lua loop_add: 437ms.

the sum is 20000000.

loop lua add: 2360ms.

the sum is 20000000.

C呼叫lua指令碼的效率測試

include extern c lua直譯器指標 const char lua script function loop add a,b local sum 0 for i 1,10000000 do sum sum a b end return sum end function add a,b ...

C呼叫lua指令碼的效率測試

c呼叫lua指令碼的效率測試 以下 以c語言為基準,測試了c呼叫lua迴圈和迴圈呼叫lua的效率。結論是不要頻繁地穿越c lua邊界.include extern c lua直譯器指標 const char lua script function loop add a,b local sum 0 f...

c 呼叫lua指令碼測試執行效率

include extern c pragma comment lib,lua51.lib 載入lua鏈結庫 lua state l 建立全域性lua物件指標 long long use lua fun lua state l,const char funname,int a,int b long ...