-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaIntf.cpp
More file actions
75 lines (61 loc) · 1.42 KB
/
LuaIntf.cpp
File metadata and controls
75 lines (61 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// LuaIntf.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <boost/timer/timer.hpp>
#include <LuaIntf/LuaIntf.h>
#include <iostream>
using namespace LuaIntf;
using namespace std;
int add(int a, int b)
{
return a + b;
}
void add_times(int a, int b, int times)
{
for (int i = 0; i < times; ++i)
add(a, b);
}
extern "C" int LuaOpenModule_Test(lua_State* L)
{
assert(L);
using namespace LuaIntf;
LuaRef mod = LuaRef::createTable(L);
LuaBinding(mod)
.addFunction("add", add)
.addFunction("add_times", add)
;
mod.pushToStack();
return 1;
}
int main()
{
LuaContext luaCtx;
lua_State* L = luaCtx.state();
LuaRef table(L, "package.preload");
table["c_test"] = LuaRef::createFunctionWith(L, LuaOpenModule_Test);
LuaRef require(L, "require");
LuaRef test = require.call<LuaRef>("test");
const int COUNT = 10000000;
cout << "C++ calls lua add() many times:\n";
{
boost::timer::auto_cpu_timer t;
for (int i = 0; i < COUNT; ++i)
test.dispatchStatic("add", 123, 456);
}
cout << "C++ calls lua add_times() once:\n";
{
boost::timer::auto_cpu_timer t;
test.dispatchStatic("add_times", 123, 456, COUNT);
}
cout << "Lua calls C++ add() many times:\n";
{
boost::timer::auto_cpu_timer t;
test.dispatchStatic("test_c_add", 123, 456, COUNT);
}
cout << "Lua calls C++ add_times() once:\n";
{
boost::timer::auto_cpu_timer t;
test.dispatchStatic("test_c_add_times", 123, 456, COUNT);
}
return 0;
}