Browse Source

Update: benchmark

AnnulusGames 1 year ago
parent
commit
c175d46b65
2 changed files with 24 additions and 2 deletions
  1. 22 0
      sandbox/Benchmark/AddBenchmark.cs
  2. 2 2
      sandbox/Benchmark/add.lua

+ 22 - 0
sandbox/Benchmark/AddBenchmark.cs

@@ -1,18 +1,40 @@
+using System.Reflection;
 using BenchmarkDotNet.Attributes;
 using BenchmarkDotNet.Attributes;
 using Lua;
 using Lua;
+using Lua.Standard;
 using MoonSharp.Interpreter;
 using MoonSharp.Interpreter;
 
 
+sealed class AddFunction : Lua.LuaFunction
+{
+    protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
+    {
+        buffer.Span[0] = context.GetArgument<double>(0) + context.GetArgument<double>(1);
+        return new(1);
+    }
+}
+
 [Config(typeof(BenchmarkConfig))]
 [Config(typeof(BenchmarkConfig))]
 public class AddBenchmark
 public class AddBenchmark
 {
 {
     BenchmarkCore core = new();
     BenchmarkCore core = new();
     LuaValue[] buffer = new LuaValue[1];
     LuaValue[] buffer = new LuaValue[1];
 
 
+    public static double Add(double x, double y)
+    {
+        return x + y;
+    }
+
     [IterationSetup]
     [IterationSetup]
     public void Setup()
     public void Setup()
     {
     {
         core = new();
         core = new();
         core.Setup("add.lua");
         core.Setup("add.lua");
+        core.LuaCSharpState.OpenStandardLibraries();
+
+        core.LuaCSharpState.Environment["add"] = new AddFunction();
+        core.MoonSharpState.Globals["add"] = (Func<double, double, double>)Add;
+        core.NLuaState.RegisterFunction("add", typeof(AddBenchmark).GetMethod(nameof(Add), BindingFlags.Static | BindingFlags.Public));
+        core.NeoLuaEnvironment.SetValue("add", (Func<double, double, double>)Add);
     }
     }
 
 
     [IterationCleanup]
     [IterationCleanup]

+ 2 - 2
sandbox/Benchmark/add.lua

@@ -1,7 +1,7 @@
 local x = 0
 local x = 0
 
 
-for i = 0, 10000 do
-    x = x + i;
+for _ = 0, 10000 do
+    x = add(x, 1)
 end
 end
 
 
 return x
 return x