/* --------------------------------------------------------- *
* __________ D E L T A S C R I P T *
* (_________() *
* / === / - A fast, dynamic scripting language *
* | == | - Version 4.13.11.0 *
* / === / - Developed by Adam R. Nelson *
* | = = | - 2011-2013 *
* / === / - Distributed under GNU LGPL v3 *
* (________() - http://github.com/ar-nelson/deltascript *
* *
* --------------------------------------------------------- */
package com.sector91.delta.script.profiling;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
import com.sector91.delta.script.*;
import com.sector91.delta.script.instrs.DSInstr;
import com.sector91.delta.script.objects.DS_Scope;
import com.sector91.delta.script.parser.DScriptParserException;
public class DeltaScriptProfiler
{
static Script[] jsScripts = null;
static DSInstr[] dsScripts = null;
public static void main(String[] args) throws Exception
{
// Run each test once to avoid initialization lag.
System.out.println("Initializing...");
testJS(); testDS(); System.gc(); System.gc();
int times = 24;
long jsTotal = 0, dsTotal = 0;
System.out.println("Testing...");
for (int i=0; i<times; i++)
{
long dsTime = testDS();
System.out.println("DeltaScript Run " + (i+1) + ": " + dsTime + "ms");
dsTotal += dsTime;
long jsTime = testJS();
System.out.println("JavaScript Run " + (i+1) + ": " + jsTime + "ms");
jsTotal += jsTime;
}
System.out.println("------------------------------------------------");
System.out.println("RESULTS: ");
System.out.println("Average JavaScript time: " + (jsTotal/times) + "ms");
System.out.println("Average DeltaScript time: " + (dsTotal/times) + "ms");
System.out.println("JS/DS Performance Ratio: " + ((float)jsTotal/(float)dsTotal));
}
private static long testJS()
{
Context cx = Context.enter();
Scriptable jsScope = cx.initStandardObjects();
if (jsScripts == null)
jsScripts = new Script[] {
cx.compileString("var a=0; for (i=0; i<1000000; i++) {a++;}", "<cmd>", 0, null),
cx.compileString("var a=1; for (i=0; i<1000000; i++) {if (a==i) {a--;}}", "<cmd>", 0, null),
cx.compileString("var arr = []; for (i=0; i<100000; i++) {arr.push(i);}", "<cmd>", 0, null),
cx.compileString("var a1 = []; for (i=0; i<500; i++) {var a2 = []; for (j=0; j<500; j++) {a2.push(j)} a1.push(a2);}", "<cmd>", 0, null),
cx.compileString("var a=0, f=function() {a++}; for(i=0;i<1000000; i++) {f();}", "<cmd>", 0, null),
};
long mark = System.currentTimeMillis();
for (Script s : jsScripts)
s.exec(cx, jsScope);
long time = System.currentTimeMillis()-mark;
return time;
}
private static long testDS() throws DScriptErr, DScriptParserException
{
DScriptContext context = new DScriptContext();
DS_Scope scope = context.createScope();
if (dsScripts == null)
dsScripts = new DSInstr[] {
DeltaScript.compile("a=0; loop i from 0 to 1000000; a++; xl", context),
DeltaScript.compile("a=1; loop i from 0 to 1000000; if a==i, a--, xi; xl", context),
DeltaScript.compile("arr=..list(); loop i from 0 to 100000; arr.push(i); xl", context),
DeltaScript.compile("a1=..list(); loop i from 0 to 500; a2=..list(); loop j from 0 to 500, a2.push(j), xl; a1.push(a2); xl", context),
DeltaScript.compile("a=0; f=-> a++; loop i from 0 to 1000000, f(), xl", context),
};
long mark = System.currentTimeMillis();
for (DSInstr s : dsScripts)
DeltaScript.exec(s, scope, context);
long time = System.currentTimeMillis()-mark;
return time;
}
}