namespace TestClearScriptPerformanceAndNesting
{
// Some poor-man's performance measurements between
// ClearScript's V8, ClearScript's JScript (Microsoft Active Scripting)
// and Jint.
//
// Related to
// https://clearscript.codeplex.com/discussions/554734
//
// If you like it, please also visit http://www.zeta-producer.com for
// a Windows-based offline Desktop CMS.
using System;
using System.Diagnostics;
using Jint;
using Microsoft.ClearScript;
using Microsoft.ClearScript.V8;
using Microsoft.ClearScript.Windows;
internal static class Program
{
private const int LoopCount = 2000;
private static void Main()
{
var sw9 = Run9(); Console.WriteLine(sw9.Elapsed);
var sw7 = Run7(); Console.WriteLine(sw7.Elapsed);
var sw8 = Run8(); Console.WriteLine(sw8.Elapsed);
var sw6 = Run6(); Console.WriteLine(sw6.Elapsed);
var sw5 = Run5(); Console.WriteLine(sw5.Elapsed);
var sw4 = Run4(); Console.WriteLine(sw4.Elapsed);
var sw3 = Run3(); Console.WriteLine(sw3.Elapsed);
var sw1 = Run1(); Console.WriteLine(sw1.Elapsed);
var sw2 = Run2(); Console.WriteLine(sw2.Elapsed);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("-");
Console.WriteLine("ClearScript JScript one interpreter per loop : {0}.", sw1.Elapsed);
Console.WriteLine("ClearScript JScript two interpreters per loop: {0}.", sw2.Elapsed);
Console.WriteLine("ClearScript JScript one interpreter : {0}.", sw6.Elapsed);
Console.WriteLine("-");
Console.WriteLine("ClearScript V8 one interpreter per loop : {0}.", sw7.Elapsed);
Console.WriteLine("ClearScript V8 two interpreters per loop : {0}.", sw8.Elapsed);
Console.WriteLine("ClearScript V8 one interpreter : {0}.", sw9.Elapsed);
Console.WriteLine("-");
Console.WriteLine("Jint two interpreters per loop : {0}.", sw3.Elapsed);
Console.WriteLine("Jint one interperter per loop : {0}.", sw4.Elapsed);
Console.WriteLine("Jint one interpreter : {0}.", sw5.Elapsed);
Console.WriteLine();
Console.WriteLine();
}
private static Stopwatch Run1()
{
var sw = new Stopwatch();
sw.Start();
for (var i = 0; i < LoopCount; i++)
{
using (var engine = new JScriptEngine())
{
engine.AddHostObject(@"host", new HostFunctions1(engine));
Trace.WriteLine(engine.Evaluate(@"host.RunIt()"));
}
}
sw.Stop();
return sw;
}
private static Stopwatch Run6()
{
var sw = new Stopwatch();
sw.Start();
using (var engine = new JScriptEngine())
{
for (var i = 0; i < LoopCount; i++)
{
engine.AddHostObject(@"host", new HostFunctions1(engine));
Trace.WriteLine(engine.Evaluate(@"host.RunIt()"));
}
}
sw.Stop();
return sw;
}
private static Stopwatch Run2()
{
var sw = new Stopwatch();
sw.Start();
for (var i = 0; i < LoopCount; i++)
{
using (var engine = new JScriptEngine())
{
engine.AddHostObject(@"host", new HostFunctions2());
Trace.WriteLine(engine.Evaluate(@"host.RunIt()"));
}
}
sw.Stop();
return sw;
}
private static Stopwatch Run7()
{
var sw = new Stopwatch();
sw.Start();
for (var i = 0; i < LoopCount; i++)
{
using (var engine = new V8ScriptEngine())
{
engine.AddHostObject(@"host", new HostFunctions1(engine));
Trace.WriteLine(engine.Evaluate(@"host.RunIt()"));
}
}
sw.Stop();
return sw;
}
private static Stopwatch Run9()
{
var sw = new Stopwatch();
sw.Start();
using (var engine = new V8ScriptEngine())
{
for (var i = 0; i < LoopCount; i++)
{
engine.AddHostObject(@"host", new HostFunctions1(engine));
Trace.WriteLine(engine.Evaluate(@"host.RunIt()"));
}
}
sw.Stop();
return sw;
}
private static Stopwatch Run8()
{
var sw = new Stopwatch();
sw.Start();
for (var i = 0; i < LoopCount; i++)
{
using (var engine = new V8ScriptEngine())
{
engine.AddHostObject(@"host", new HostFunctions5());
Trace.WriteLine(engine.Evaluate(@"host.RunIt()"));
}
}
sw.Stop();
return sw;
}
private static Stopwatch Run3()
{
var sw = new Stopwatch();
sw.Start();
for (var i = 0; i < LoopCount; i++)
{
var engine = new Engine();
engine.SetValue(@"host", new HostFunctions3());
Trace.WriteLine(engine.Execute(@"host.RunIt()").GetCompletionValue().ToObject());
}
sw.Stop();
return sw;
}
private static Stopwatch Run4()
{
var sw = new Stopwatch();
sw.Start();
for (var i = 0; i < LoopCount; i++)
{
var engine = new Engine();
engine.SetValue(@"host", new HostFunctions4(engine));
Trace.WriteLine(engine.Execute(@"host.RunIt()").GetCompletionValue().ToObject());
}
sw.Stop();
return sw;
}
private static Stopwatch Run5()
{
var sw = new Stopwatch();
sw.Start();
var engine = new Engine();
for (var i = 0; i < LoopCount; i++)
{
engine.SetValue(@"host", new HostFunctions4(engine));
Trace.WriteLine(engine.Execute(@"host.RunIt()").GetCompletionValue().ToObject());
}
sw.Stop();
return sw;
}
}
public sealed class HostFunctions1
{
private readonly ScriptEngine _engine;
public HostFunctions1(ScriptEngine engine)
{
_engine = engine;
}
public object RunIt()
{
return _engine.Evaluate(@"1+2");
}
}
public sealed class HostFunctions2
{
public object RunIt()
{
using (var engine = new JScriptEngine())
{
return engine.Evaluate(@"1+2");
}
}
}
public sealed class HostFunctions5
{
public object RunIt()
{
using (var engine = new V8ScriptEngine())
{
return engine.Evaluate(@"1+2");
}
}
}
public sealed class HostFunctions3
{
public object RunIt()
{
var engine = new Engine();
return engine.Execute(@"1+2").GetCompletionValue();
}
}
public sealed class HostFunctions4
{
private readonly Engine _engine;
public HostFunctions4(Engine engine)
{
_engine = engine;
}
public object RunIt()
{
return _engine.Execute(@"1+2").GetCompletionValue();
}
}
}
Comments