Advertisement
uwekeim

Performance of V8 (ClearScript) vs. JScript vs Jint

Aug 2nd, 2014
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.06 KB | None | 0 0
  1. namespace TestClearScriptPerformanceAndNesting
  2. {
  3.     // Some poor-man's performance measurements between
  4.     // ClearScript's V8, ClearScript's JScript (Microsoft Active Scripting)
  5.     // and Jint.
  6.     //
  7.     // Related to
  8.     // https://clearscript.codeplex.com/discussions/554734
  9.     //
  10.     // If you like it, please also visit http://www.zeta-producer.com for
  11.     // a Windows-based offline Desktop CMS.
  12.  
  13.     using System;
  14.     using System.Diagnostics;
  15.     using Jint;
  16.     using Microsoft.ClearScript;
  17.     using Microsoft.ClearScript.V8;
  18.     using Microsoft.ClearScript.Windows;
  19.  
  20.     internal static class Program
  21.     {
  22.         private const int LoopCount = 2000;
  23.  
  24.         private static void Main()
  25.         {
  26.             var sw9 = Run9(); Console.WriteLine(sw9.Elapsed);
  27.             var sw7 = Run7(); Console.WriteLine(sw7.Elapsed);
  28.             var sw8 = Run8(); Console.WriteLine(sw8.Elapsed);
  29.             var sw6 = Run6(); Console.WriteLine(sw6.Elapsed);
  30.             var sw5 = Run5(); Console.WriteLine(sw5.Elapsed);
  31.             var sw4 = Run4(); Console.WriteLine(sw4.Elapsed);
  32.             var sw3 = Run3(); Console.WriteLine(sw3.Elapsed);
  33.             var sw1 = Run1(); Console.WriteLine(sw1.Elapsed);
  34.             var sw2 = Run2(); Console.WriteLine(sw2.Elapsed);
  35.  
  36.             Console.WriteLine();
  37.             Console.WriteLine();
  38.             Console.WriteLine();
  39.             Console.WriteLine("-");
  40.             Console.WriteLine("ClearScript JScript one interpreter per loop : {0}.", sw1.Elapsed);
  41.             Console.WriteLine("ClearScript JScript two interpreters per loop: {0}.", sw2.Elapsed);
  42.             Console.WriteLine("ClearScript JScript one interpreter          : {0}.", sw6.Elapsed);
  43.             Console.WriteLine("-");
  44.             Console.WriteLine("ClearScript V8 one interpreter per loop      : {0}.", sw7.Elapsed);
  45.             Console.WriteLine("ClearScript V8 two interpreters per loop     : {0}.", sw8.Elapsed);
  46.             Console.WriteLine("ClearScript V8 one interpreter               : {0}.", sw9.Elapsed);
  47.             Console.WriteLine("-");
  48.             Console.WriteLine("Jint two interpreters per loop               : {0}.", sw3.Elapsed);
  49.             Console.WriteLine("Jint one interperter per loop                : {0}.", sw4.Elapsed);
  50.             Console.WriteLine("Jint one interpreter                         : {0}.", sw5.Elapsed);
  51.             Console.WriteLine();
  52.             Console.WriteLine();
  53.         }
  54.  
  55.         private static Stopwatch Run1()
  56.         {
  57.             var sw = new Stopwatch();
  58.             sw.Start();
  59.             for (var i = 0; i < LoopCount; i++)
  60.             {
  61.                 using (var engine = new JScriptEngine())
  62.                 {
  63.                     engine.AddHostObject(@"host", new HostFunctions1(engine));
  64.                     Trace.WriteLine(engine.Evaluate(@"host.RunIt()"));
  65.                 }
  66.             }
  67.             sw.Stop();
  68.             return sw;
  69.         }
  70.  
  71.         private static Stopwatch Run6()
  72.         {
  73.             var sw = new Stopwatch();
  74.             sw.Start();
  75.             using (var engine = new JScriptEngine())
  76.             {
  77.                 for (var i = 0; i < LoopCount; i++)
  78.                 {
  79.                     engine.AddHostObject(@"host", new HostFunctions1(engine));
  80.                     Trace.WriteLine(engine.Evaluate(@"host.RunIt()"));
  81.                 }
  82.             }
  83.             sw.Stop();
  84.             return sw;
  85.         }
  86.  
  87.         private static Stopwatch Run2()
  88.         {
  89.             var sw = new Stopwatch();
  90.             sw.Start();
  91.             for (var i = 0; i < LoopCount; i++)
  92.             {
  93.                 using (var engine = new JScriptEngine())
  94.                 {
  95.                     engine.AddHostObject(@"host", new HostFunctions2());
  96.                     Trace.WriteLine(engine.Evaluate(@"host.RunIt()"));
  97.                 }
  98.             }
  99.             sw.Stop();
  100.             return sw;
  101.         }
  102.  
  103.         private static Stopwatch Run7()
  104.         {
  105.             var sw = new Stopwatch();
  106.             sw.Start();
  107.             for (var i = 0; i < LoopCount; i++)
  108.             {
  109.                 using (var engine = new V8ScriptEngine())
  110.                 {
  111.                     engine.AddHostObject(@"host", new HostFunctions1(engine));
  112.                     Trace.WriteLine(engine.Evaluate(@"host.RunIt()"));
  113.                 }
  114.             }
  115.             sw.Stop();
  116.             return sw;
  117.         }
  118.  
  119.         private static Stopwatch Run9()
  120.         {
  121.             var sw = new Stopwatch();
  122.             sw.Start();
  123.             using (var engine = new V8ScriptEngine())
  124.             {
  125.                 for (var i = 0; i < LoopCount; i++)
  126.                 {
  127.                     engine.AddHostObject(@"host", new HostFunctions1(engine));
  128.                     Trace.WriteLine(engine.Evaluate(@"host.RunIt()"));
  129.                 }
  130.             }
  131.             sw.Stop();
  132.             return sw;
  133.         }
  134.  
  135.         private static Stopwatch Run8()
  136.         {
  137.             var sw = new Stopwatch();
  138.             sw.Start();
  139.             for (var i = 0; i < LoopCount; i++)
  140.             {
  141.                 using (var engine = new V8ScriptEngine())
  142.                 {
  143.                     engine.AddHostObject(@"host", new HostFunctions5());
  144.                     Trace.WriteLine(engine.Evaluate(@"host.RunIt()"));
  145.                 }
  146.             }
  147.             sw.Stop();
  148.             return sw;
  149.         }
  150.  
  151.         private static Stopwatch Run3()
  152.         {
  153.             var sw = new Stopwatch();
  154.             sw.Start();
  155.             for (var i = 0; i < LoopCount; i++)
  156.             {
  157.                 var engine = new Engine();
  158.                 engine.SetValue(@"host", new HostFunctions3());
  159.                 Trace.WriteLine(engine.Execute(@"host.RunIt()").GetCompletionValue().ToObject());
  160.             }
  161.             sw.Stop();
  162.             return sw;
  163.         }
  164.  
  165.         private static Stopwatch Run4()
  166.         {
  167.             var sw = new Stopwatch();
  168.             sw.Start();
  169.             for (var i = 0; i < LoopCount; i++)
  170.             {
  171.                 var engine = new Engine();
  172.                 engine.SetValue(@"host", new HostFunctions4(engine));
  173.                 Trace.WriteLine(engine.Execute(@"host.RunIt()").GetCompletionValue().ToObject());
  174.             }
  175.             sw.Stop();
  176.             return sw;
  177.         }
  178.  
  179.         private static Stopwatch Run5()
  180.         {
  181.             var sw = new Stopwatch();
  182.             sw.Start();
  183.             var engine = new Engine();
  184.             for (var i = 0; i < LoopCount; i++)
  185.             {
  186.                 engine.SetValue(@"host", new HostFunctions4(engine));
  187.                 Trace.WriteLine(engine.Execute(@"host.RunIt()").GetCompletionValue().ToObject());
  188.             }
  189.             sw.Stop();
  190.             return sw;
  191.         }
  192.     }
  193.  
  194.     public sealed class HostFunctions1
  195.     {
  196.         private readonly ScriptEngine _engine;
  197.  
  198.         public HostFunctions1(ScriptEngine engine)
  199.         {
  200.             _engine = engine;
  201.         }
  202.  
  203.         public object RunIt()
  204.         {
  205.             return _engine.Evaluate(@"1+2");
  206.         }
  207.     }
  208.  
  209.     public sealed class HostFunctions2
  210.     {
  211.         public object RunIt()
  212.         {
  213.             using (var engine = new JScriptEngine())
  214.             {
  215.                 return engine.Evaluate(@"1+2");
  216.             }
  217.         }
  218.     }
  219.  
  220.     public sealed class HostFunctions5
  221.     {
  222.         public object RunIt()
  223.         {
  224.             using (var engine = new V8ScriptEngine())
  225.             {
  226.                 return engine.Evaluate(@"1+2");
  227.             }
  228.         }
  229.     }
  230.  
  231.     public sealed class HostFunctions3
  232.     {
  233.         public object RunIt()
  234.         {
  235.             var engine = new Engine();
  236.             return engine.Execute(@"1+2").GetCompletionValue();
  237.         }
  238.     }
  239.  
  240.     public sealed class HostFunctions4
  241.     {
  242.         private readonly Engine _engine;
  243.  
  244.         public HostFunctions4(Engine engine)
  245.         {
  246.             _engine = engine;
  247.         }
  248.  
  249.         public object RunIt()
  250.         {
  251.             return _engine.Execute(@"1+2").GetCompletionValue();
  252.         }
  253.     }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement