Program.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // This code is a workbench code - it gets commented on the fly, changed, etc.
  2. // Disable warnings for "assigned but value never used" and "unreachable code".
  3. #pragma warning disable 414
  4. #pragma warning disable 429
  5. //#define PROFILER
  6. using System;
  7. using System.Collections.Generic;
  8. using MoonSharp.Interpreter.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using MoonSharp.Interpreter;
  13. using MoonSharp.Interpreter.Execution;
  14. using NLua;
  15. using System.Diagnostics;
  16. namespace PerformanceComparison
  17. {
  18. class Program
  19. {
  20. #if PROFILER
  21. const int ITERATIONS = 10;
  22. #else
  23. const int ITERATIONS = 1;
  24. #endif
  25. static string scriptText = @"
  26. function move(n, src, dst, via)
  27. if n > 0 then
  28. move(n - 1, src, via, dst)
  29. print(src, 'to', dst)
  30. move(n - 1, via, dst, src)
  31. end
  32. end
  33. for i = 1, 10000 do
  34. move(4, 1, 2, 3)
  35. end
  36. ";
  37. static string scriptText22 = @"
  38. N = 8
  39. board = {}
  40. for i = 1, N do
  41. board[i] = {}
  42. for j = 1, N do
  43. board[i][j] = false
  44. end
  45. end
  46. function Allowed( x, y )
  47. for i = 1, x-1 do
  48. if ( board[i][y] ) or ( i <= y and board[x-i][y-i] ) or ( y+i <= N and board[x-i][y+i] ) then
  49. return false
  50. end
  51. end
  52. return true
  53. end
  54. function Find_Solution( x )
  55. for y = 1, N do
  56. if Allowed( x, y ) then
  57. board[x][y] = true
  58. if x == N or Find_Solution( x+1 ) then
  59. return true
  60. end
  61. board[x][y] = false
  62. end
  63. end
  64. return false
  65. end
  66. if Find_Solution( 1 ) then
  67. for i = 1, N do
  68. for j = 1, N do
  69. if board[i][j] then
  70. --print( 'Q' )
  71. else
  72. --print( 'x' )
  73. end
  74. end
  75. --print( '|' )
  76. end
  77. else
  78. --print( 'NO!' )
  79. end
  80. ";
  81. static StringBuilder g_MoonSharpStr = new StringBuilder();
  82. static StringBuilder g_NLuaStr = new StringBuilder();
  83. public static DynValue Print(ScriptExecutionContext executionContext, CallbackArguments values)
  84. {
  85. foreach (var val in values.GetArray())
  86. {
  87. g_MoonSharpStr.Append(val.ToPrintString());
  88. }
  89. g_MoonSharpStr.AppendLine();
  90. return DynValue.Nil;
  91. }
  92. public static void NPrint(params object[] values)
  93. {
  94. foreach (var val in values)
  95. {
  96. g_NLuaStr.Append(val.ToString());
  97. }
  98. g_NLuaStr.AppendLine();
  99. }
  100. public static void PrintX(int from, string mid, int to)
  101. {
  102. g_MoonSharpStr.Append(from);
  103. g_MoonSharpStr.Append(mid);
  104. g_MoonSharpStr.Append(to);
  105. g_MoonSharpStr.AppendLine();
  106. }
  107. static Lua lua = new Lua();
  108. static string testString = "world";
  109. static void xxMain(string[] args)
  110. {
  111. Stopwatch sw;
  112. sw = Stopwatch.StartNew();
  113. var _s = new Script();
  114. _s.LoadString(scriptText);
  115. sw.Stop();
  116. Console.WriteLine("Build : {0} ms", sw.ElapsedMilliseconds);
  117. sw = Stopwatch.StartNew();
  118. var script = new Script();
  119. script.Globals.Set("print", DynValue.NewCallback(new CallbackFunction(Print)));
  120. CallbackFunction.DefaultAccessMode = InteropAccessMode.Preoptimized;
  121. //script.Globals["print"] = (Action<int, string, int>)PrintX;
  122. DynValue func = script.LoadString(scriptText);
  123. sw.Stop();
  124. Console.WriteLine("Build 2: {0} ms", sw.ElapsedMilliseconds);
  125. sw = Stopwatch.StartNew();
  126. for (int i = 0; i < ITERATIONS; i++)
  127. {
  128. script.Call(func);
  129. }
  130. sw.Stop();
  131. Console.WriteLine("MoonSharp : {0} ms", sw.ElapsedMilliseconds);
  132. lua.RegisterFunction("print", typeof(Program).GetMethod("NPrint"));
  133. File.WriteAllText(@"c:\temp\hanoi.lua", scriptText);
  134. #if !PROFILER
  135. var fn = lua.LoadFile(@"c:\temp\hanoi.lua");
  136. sw = Stopwatch.StartNew();
  137. for (int i = 0; i < ITERATIONS; i++)
  138. {
  139. fn.Call();
  140. }
  141. sw.Stop();
  142. #endif
  143. Console.WriteLine("NLua : {0} ms", sw.ElapsedMilliseconds);
  144. Console.WriteLine("M# == NL ? {0}", g_MoonSharpStr.ToString() == g_NLuaStr.ToString());
  145. Console.WriteLine("=== MoonSharp ===");
  146. //Console.WriteLine(g_MoonSharpStr.ToString());
  147. Console.WriteLine("");
  148. Console.WriteLine("=== NLua ===");
  149. //Console.WriteLine(g_NLuaStr.ToString());
  150. Console.ReadKey();
  151. }
  152. }
  153. }