| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- // This code is a workbench code - it gets commented on the fly, changed, etc.
- // Disable warnings for "assigned but value never used" and "unreachable code".
- #pragma warning disable 414
- #pragma warning disable 429
- //#define PROFILER
- using System;
- using System.Collections.Generic;
- using MoonSharp.Interpreter.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using MoonSharp.Interpreter;
- using MoonSharp.Interpreter.Execution;
- using NLua;
- using System.Diagnostics;
- namespace PerformanceComparison
- {
- class Program
- {
- #if PROFILER
- const int ITERATIONS = 10;
- #else
- const int ITERATIONS = 1;
- #endif
- static string scriptText = @"
- function move(n, src, dst, via)
- if n > 0 then
- move(n - 1, src, via, dst)
- print(src, 'to', dst)
- move(n - 1, via, dst, src)
- end
- end
-
- for i = 1, 10000 do
- move(4, 1, 2, 3)
- end
- ";
- static string scriptText22 = @"
- N = 8
-
- board = {}
- for i = 1, N do
- board[i] = {}
- for j = 1, N do
- board[i][j] = false
- end
- end
-
- function Allowed( x, y )
- for i = 1, x-1 do
- if ( board[i][y] ) or ( i <= y and board[x-i][y-i] ) or ( y+i <= N and board[x-i][y+i] ) then
- return false
- end
- end
- return true
- end
-
- function Find_Solution( x )
- for y = 1, N do
- if Allowed( x, y ) then
- board[x][y] = true
- if x == N or Find_Solution( x+1 ) then
- return true
- end
- board[x][y] = false
- end
- end
- return false
- end
-
- if Find_Solution( 1 ) then
- for i = 1, N do
- for j = 1, N do
- if board[i][j] then
- --print( 'Q' )
- else
- --print( 'x' )
- end
- end
- --print( '|' )
- end
- else
- --print( 'NO!' )
- end
-
- ";
- static StringBuilder g_MoonSharpStr = new StringBuilder();
- static StringBuilder g_NLuaStr = new StringBuilder();
- public static DynValue Print(ScriptExecutionContext executionContext, CallbackArguments values)
- {
- foreach (var val in values.GetArray())
- {
- g_MoonSharpStr.Append(val.ToPrintString());
- }
- g_MoonSharpStr.AppendLine();
- return DynValue.Nil;
- }
- public static void NPrint(params object[] values)
- {
- foreach (var val in values)
- {
- g_NLuaStr.Append(val.ToString());
- }
- g_NLuaStr.AppendLine();
- }
- public static void PrintX(int from, string mid, int to)
- {
- g_MoonSharpStr.Append(from);
- g_MoonSharpStr.Append(mid);
- g_MoonSharpStr.Append(to);
- g_MoonSharpStr.AppendLine();
- }
- static Lua lua = new Lua();
- static string testString = "world";
- static void xxMain(string[] args)
- {
- Stopwatch sw;
- sw = Stopwatch.StartNew();
- var _s = new Script();
- _s.LoadString(scriptText);
- sw.Stop();
- Console.WriteLine("Build : {0} ms", sw.ElapsedMilliseconds);
- sw = Stopwatch.StartNew();
- var script = new Script();
- script.Globals.Set("print", DynValue.NewCallback(new CallbackFunction(Print)));
- CallbackFunction.DefaultAccessMode = InteropAccessMode.Preoptimized;
- //script.Globals["print"] = (Action<int, string, int>)PrintX;
- DynValue func = script.LoadString(scriptText);
- sw.Stop();
- Console.WriteLine("Build 2: {0} ms", sw.ElapsedMilliseconds);
- sw = Stopwatch.StartNew();
- for (int i = 0; i < ITERATIONS; i++)
- {
- script.Call(func);
- }
- sw.Stop();
- Console.WriteLine("MoonSharp : {0} ms", sw.ElapsedMilliseconds);
- lua.RegisterFunction("print", typeof(Program).GetMethod("NPrint"));
- File.WriteAllText(@"c:\temp\hanoi.lua", scriptText);
- #if !PROFILER
- var fn = lua.LoadFile(@"c:\temp\hanoi.lua");
- sw = Stopwatch.StartNew();
- for (int i = 0; i < ITERATIONS; i++)
- {
- fn.Call();
- }
- sw.Stop();
- #endif
- Console.WriteLine("NLua : {0} ms", sw.ElapsedMilliseconds);
- Console.WriteLine("M# == NL ? {0}", g_MoonSharpStr.ToString() == g_NLuaStr.ToString());
- Console.WriteLine("=== MoonSharp ===");
- //Console.WriteLine(g_MoonSharpStr.ToString());
- Console.WriteLine("");
- Console.WriteLine("=== NLua ===");
- //Console.WriteLine(g_NLuaStr.ToString());
- Console.ReadKey();
- }
- }
- }
|