1 #include <iostream>
2 #include <ctime>
3 
4 namespace std
5 {
6 	using ::clock_t;
7 //	using ::clock;
8 }
9 
10 #define LUABIND_NO_ERROR_CHECKING
11 #define LUABIND_DONT_COPY_STRINGS
12 //#define LUABIND_NOT_THREADSAFE
13 
14 extern "C"
15 {
16 	#include "lua.h"
17 	#include "lauxlib.h"
18 }
19 
20 #include <luabind/luabind.hpp>
21 
22 struct A {};
23 
24 // luabind function
f1(int a,float b,const char * str,A * c)25 float f1(int a, float b, const char* str, A* c)
26 {
27 	return 3.14f;
28 }
29 
30 // empty function
f2(lua_State * L)31 int f2(lua_State* L)
32 {
33 	return 0;
34 }
35 
36 
main()37 int main()
38 {
39 	const int num_calls = 100000;
40 	const int loops = 10;
41 
42 	using namespace luabind;
43 
44 	lua_State* L = lua_open();
45 	open(L);
46 
47 	class_<A>(L, "A")
48 		.def(constructor<>());
49 
50 	function(L, "test1", &f1);
51 
52 	lua_pushstring(L, "test2");
53 	lua_pushcclosure(L, &f2, 0);
54 	lua_settable(L, LUA_GLOBALSINDEX);
55 
56 	std::clock_t total1 = 0;
57 	std::clock_t total2 = 0;
58 
59 	for (int i = 0; i < loops; ++i)
60 	{
61 		// benchmark luabind
62 		std::clock_t start1 = std::clock();
63 		lua_dostring(L, "a = A()\n"
64 									"for i = 1, 100000 do\n"
65 										"test1(5, 4.6, 'foo', a)\n"
66 									"end");
67 
68 		std::clock_t end1 = std::clock();
69 
70 
71 		// benchmark empty binding
72 		std::clock_t start2 = std::clock();
73 		lua_dostring(L, "a = A()\n"
74 									"for i = 1, 100000 do\n"
75 										"test2(5, 4.6, 'foo', a)\n"
76 									"end");
77 
78 		std::clock_t end2 = std::clock();
79 		total1 += end1 - start1;
80 		total2 += end2 - start2;
81 	}
82 
83 
84 	double time1 = double(total1) / (double)CLOCKS_PER_SEC;
85 	double time2 = double(total2) / (double)CLOCKS_PER_SEC;
86 
87 #ifdef LUABIND_NO_ERROR_CHECKING
88 	std::cout << "without error-checking\n";
89 #endif
90 	std::cout << "luabind:\t" << time1 * 1000000 / num_calls / loops << " microseconds per call\n"
91 		<< "empty:\t" << time2 * 1000000 / num_calls / loops << " microseconds per call\n"
92 		<< "diff:\t" << ((time1 - time2) * 1000000 / num_calls / loops) << " microseconds\n\n";
93 
94 	lua_close(L);
95 }
96 
97