File: benchmark.cpp

package info (click to toggle)
luabind 0.9.1%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,844 kB
  • ctags: 2,833
  • sloc: cpp: 13,796; makefile: 126; sh: 24; ansic: 11
file content (95 lines) | stat: -rw-r--r-- 1,791 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <ctime>

namespace std
{
	using ::clock_t;
//	using ::clock;
}

#define LUABIND_NO_ERROR_CHECKING
#define LUABIND_DONT_COPY_STRINGS
//#define LUABIND_NOT_THREADSAFE

extern "C"
{
	#include "lua.h"
	#include "lauxlib.h"
}

#include <luabind/luabind.hpp>

struct A {};

// luabind function
float f1(int a, float b, const char* str, A* c)
{
	return 3.14f;
}

// empty function
int f2(lua_State* L)
{
	return 0;
}


int main()
{
	const int num_calls = 100000;
	const int loops = 10;

	using namespace luabind;

	lua_State* L = luaL_newstate();
	open(L);
	
	class_<A>(L, "A")
		.def(constructor<>());

	function(L, "test1", &f1);

	lua_pushcclosure(L, &f2, 0);
	lua_setglobal(L, "test2");

	std::clock_t total1 = 0;
	std::clock_t total2 = 0;

	for (int i = 0; i < loops; ++i)
	{
		// benchmark luabind
		std::clock_t start1 = std::clock();
		lua_dostring(L, "a = A()\n"
									"for i = 1, 100000 do\n"
										"test1(5, 4.6, 'foo', a)\n"
									"end");

		std::clock_t end1 = std::clock();


		// benchmark empty binding
		std::clock_t start2 = std::clock();
		lua_dostring(L, "a = A()\n"
									"for i = 1, 100000 do\n"
										"test2(5, 4.6, 'foo', a)\n"
									"end");

		std::clock_t end2 = std::clock();
		total1 += end1 - start1;
		total2 += end2 - start2;
	}


	double time1 = double(total1) / (double)CLOCKS_PER_SEC;
	double time2 = double(total2) / (double)CLOCKS_PER_SEC;

#ifdef LUABIND_NO_ERROR_CHECKING
	std::cout << "without error-checking\n";
#endif
	std::cout << "luabind:\t" << time1 * 1000000 / num_calls / loops << " microseconds per call\n"
		<< "empty:\t" << time2 * 1000000 / num_calls / loops << " microseconds per call\n"
		<< "diff:\t" << ((time1 - time2) * 1000000 / num_calls / loops) << " microseconds\n\n";

	lua_close(L);
}