-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLuaNETBenchmark.cs
More file actions
104 lines (75 loc) · 2.6 KB
/
LuaNETBenchmark.cs
File metadata and controls
104 lines (75 loc) · 2.6 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using LuaNET.Lua54;
using ScriptingBenchmark.Shared;
namespace ScriptingBenchmark.LuaNET;
public class LuaNETBenchmark : IBenchmarkableAsync
{
public int LoopCount { get; private set; }
private string? _CSharpToLangCode;
private string? _LangToCSharpCode;
private string? _LangAllocCode;
private lua_State L;
public LuaNETBenchmark(int loopCount)
{
LoopCount = loopCount;
}
public void Setup()
{
_CSharpToLangCode = Codes.GetLuaCSharpToLang();
_LangToCSharpCode = Codes.GetLuaLangToCSharp(LoopCount);
_LangAllocCode = Codes.GetLuaAlloc(LoopCount);
L = Lua.luaL_newstate();
Lua.luaL_openlibs(L);
Lua.lua_pushcfunction(L, IncrementFunction);
Lua.lua_setglobal(L, "increment");
}
public void Cleanup()
{
Lua.lua_close(L);
}
public int CSharpToLang()
{
var lastTop = Lua.lua_gettop(L);
Lua.luaL_loadstring(L, _CSharpToLangCode!);
Lua.lua_pcall(L, 0, 1, 0);
int number = 0;
for (int i = 0; i < LoopCount; i++)
{
Lua.lua_pushvalue(L, -1);
Lua.lua_pushnumber(L, number);
Lua.lua_pcall(L, 1, 1, 0);
number = (int)Lua.lua_tonumber(L, -1);
Lua.lua_pop(L, 1);
}
Lua.lua_settop(L, lastTop);
return number;
}
public int LangToCSharp()
{
var lastTop = Lua.lua_gettop(L);
Lua.luaL_loadstring(L, _LangToCSharpCode!);
Lua.lua_pcall(L, 0, 1, 0);
var number = (int)Lua.lua_tonumber(L, -1);
Lua.lua_settop(L, lastTop);
return number;
}
public string LangAlloc()
{
var lastTop = Lua.lua_gettop(L);
Lua.luaL_loadstring(L, _LangAllocCode!);
Lua.lua_pcall(L, 0, 1, 0);
Lua.lua_rawgeti(L, -1, LoopCount);
Lua.lua_getfield(L, -1, "test");
var result = Lua.lua_tostring(L, -1);
Lua.lua_settop(L, lastTop);
return result!;
}
public Task<int> CSharpToLangAsync() => Task.FromResult(CSharpToLang());
public Task<int> LangToCSharpAsync() => Task.FromResult(LangToCSharp());
public Task<string> LangAllocAsync() => Task.FromResult(LangAlloc());
private static int IncrementFunction(lua_State L)
{
double number = Lua.lua_tonumber(L, 1);
Lua.lua_pushnumber(L, number + 1);
return 1; // Number of return values
}
}