Created
September 8, 2019 01:10
-
-
Save CannibalVox/aa63029545ef7fa0e8f36a43536df490 to your computer and use it in GitHub Desktop.
NLua vs. MoonSharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using MoonSharp.Interpreter; | |
namespace MoonSharpTest | |
{ | |
class Program | |
{ | |
private static int Add(int value1, int value2) | |
{ | |
return value1 + value2; | |
} | |
static void Main(string[] args) | |
{ | |
Script.WarmUp(); | |
string script = @" | |
function fib(n) | |
if n < 2 then | |
return n | |
end | |
return Add(fib(n - 2), fib(n - 1)); | |
end | |
"; | |
Script scriptObj = new Script(); | |
scriptObj.DoString(script); | |
scriptObj.Globals["Add"] = (Func<int, int, int>)Add; | |
Stopwatch sw = new Stopwatch(); | |
DynValue res = null; | |
sw.Start(); | |
for (int i = 0; i < 10000; i++) | |
{ | |
res = scriptObj.Call(scriptObj.Globals["fib"], 15); | |
} | |
sw.Stop(); | |
Console.WriteLine(res.Number); | |
Console.WriteLine("Elapsed={0}", sw.Elapsed); | |
Console.ReadKey(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Linq; | |
using NLua; | |
namespace CLuaTest | |
{ | |
class Program | |
{ | |
public static int Add(int value1, int value2) | |
{ | |
return value1 + value2; | |
} | |
static void Main(string[] args) | |
{ | |
Lua state = new Lua(); | |
state.LoadCLRPackage(); | |
state.DoString(@" | |
import ('CLuaTest', 'CLuaTest') | |
"); | |
state.DoString(@" | |
function fib(n) | |
if n < 2 then | |
return n | |
end | |
return Program.Add(fib(n - 2), fib(n - 1)); | |
end | |
"); | |
var scriptFunc = state["fib"] as LuaFunction; | |
Stopwatch sw = new Stopwatch(); | |
long res = 0; | |
sw.Start(); | |
for (var i=0; i < 10000; i++) | |
{ | |
res = (long)scriptFunc.Call(15).First(); | |
} | |
sw.Stop(); | |
Console.WriteLine(res); | |
Console.WriteLine("Elapsed={0}", sw.Elapsed); | |
Console.ReadKey(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using MoonSharp.Interpreter; | |
namespace MoonSharpTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Script.WarmUp(); | |
string script = @" | |
function fib(n) | |
if n < 2 then | |
return n | |
end | |
return fib(n - 2) + fib(n - 1); | |
end | |
"; | |
Script scriptObj = new Script(); | |
scriptObj.DoString(script); | |
Stopwatch sw = new Stopwatch(); | |
DynValue res = null; | |
sw.Start(); | |
for (int i = 0; i < 10000; i++) | |
{ | |
res = scriptObj.Call(scriptObj.Globals["fib"], 15); | |
} | |
sw.Stop(); | |
Console.WriteLine(res.Number); | |
Console.WriteLine("Elapsed={0}", sw.Elapsed); | |
Console.ReadKey(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Linq; | |
using NLua; | |
namespace CLuaTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Lua state = new Lua(); | |
state.DoString(@" | |
function fib(n) | |
if n < 2 then | |
return n | |
end | |
return fib(n - 2) + fib(n - 1); | |
end | |
"); | |
var scriptFunc = state["fib"] as LuaFunction; | |
Stopwatch sw = new Stopwatch(); | |
long res = 0; | |
sw.Start(); | |
for (var i=0; i < 10000; i++) | |
{ | |
res = (long)scriptFunc.Call(15).First(); | |
} | |
sw.Stop(); | |
Console.WriteLine(res); | |
Console.WriteLine("Elapsed={0}", sw.Elapsed); | |
Console.ReadKey(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using MoonSharp.Interpreter; | |
namespace MoonSharpTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Script.WarmUp(); | |
string script = @" | |
function fib(n) | |
if n < 2 then | |
return n | |
end | |
return fib(n - 2) + fib(n - 1); | |
end | |
"; | |
Script scriptObj = new Script(); | |
scriptObj.DoString(script); | |
Stopwatch sw = new Stopwatch(); | |
DynValue res = null; | |
sw.Start(); | |
res = scriptObj.Call(scriptObj.Globals["fib"], 35); | |
sw.Stop(); | |
Console.WriteLine(res.Number); | |
Console.WriteLine("Elapsed={0}", sw.Elapsed); | |
Console.ReadKey(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Linq; | |
using NLua; | |
namespace CLuaTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Lua state = new Lua(); | |
state.DoString(@" | |
function fib(n) | |
if n < 2 then | |
return n | |
end | |
return fib(n - 2) + fib(n - 1); | |
end | |
"); | |
var scriptFunc = state["fib"] as LuaFunction; | |
Stopwatch sw = new Stopwatch(); | |
sw.Start(); | |
var res = (long)scriptFunc.Call(35).First(); | |
sw.Stop(); | |
Console.WriteLine(res); | |
Console.WriteLine("Elapsed={0}", sw.Elapsed); | |
Console.ReadKey(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fib(35) NLua - 00:00:01.0882426 | |
fib(35) MoonSharp - 00:00:13.2349632 | |
fib(15)x10k NLua - 00:00:00.7859748 | |
fib(15)x10k MoonSharp - 00:00:08.7014046 | |
fib(15)x10k+Binding NLua - 00:00:21.9166036 | |
fib(15)x10k+Binding MoonSharp - 00:00:14.6797089 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great, thanks for the tests!