*** empty log message ***
[luabind.git] / test / benchmark.cpp
blob0b66ed9864f8fb00d2267a65999ed2f10dab29b7
1 #include <iostream>
2 #include <ctime>
4 namespace std
6 using ::clock_t;
7 // using ::clock;
10 #define LUABIND_NO_ERROR_CHECKING
11 #define LUABIND_DONT_COPY_STRINGS
12 //#define LUABIND_NOT_THREADSAFE
14 extern "C"
16 #include "lua.h"
17 #include "lauxlib.h"
20 #include <luabind/luabind.hpp>
22 struct A {};
24 // luabind function
25 float f1(int a, float b, const char* str, A* c)
27 return 3.14f;
30 // empty function
31 int f2(lua_State* L)
33 return 0;
37 int main()
39 const int num_calls = 100000;
40 const int loops = 10;
42 using namespace luabind;
44 lua_State* L = lua_open();
45 open(L);
47 class_<A>(L, "A")
48 .def(constructor<>());
50 function(L, "test1", &f1);
52 lua_pushstring(L, "test2");
53 lua_pushcclosure(L, &f2, 0);
54 lua_settable(L, LUA_GLOBALSINDEX);
56 std::clock_t total1 = 0;
57 std::clock_t total2 = 0;
59 for (int i = 0; i < loops; ++i)
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");
68 std::clock_t end1 = std::clock();
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");
78 std::clock_t end2 = std::clock();
79 total1 += end1 - start1;
80 total2 += end2 - start2;
84 double time1 = double(total1) / (double)CLOCKS_PER_SEC;
85 double time2 = double(total2) / (double)CLOCKS_PER_SEC;
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";
94 lua_close(L);