import edu.mit.ai.psg.jeva.*;
@author
public class DemoEvalWithEnv {
public static int runByReparsingForEachEval(int nExprs, int nEvals) {
int total = 0;
try {
for (int iExpr = 0; iExpr < nExprs; iExpr++) {
for (int iEval = 0; iEval < nEvals; iEval++) {
int pr = 100*(1+iEval);
double tx = 0.03+(0.01*iEval);
Integer result = (Integer)
Jeva.parseEvalStringExpression("(int)("+pr+"*(1.0+"+tx+"))");
total += result.intValue();
}
}
}
catch (ParseException e) { throw new Error(""+e); }
catch (VerifyingException e) { throw new Error(""+e); }
catch (ThrowException e) { throw new Error(""+e); }
return total;
}
public static int runByMakingEnvForEachEval(int nExprs, int nEvals) {
int total = 0;
IEnv defaultEnv = Jeva.makeDefaultEnv();
try {
for (int iExpr = 0; iExpr < nExprs; iExpr++) {
IExpressionNode expr =
Jeva.parseStringExpression("(int)(price*(1.0+tax))");
for (int iEval = 0; iEval < nEvals; iEval++) {
int pr = 100*(1+iEval);
double tx = 0.03+(0.01*iEval);
Integer result = (Integer)
EvalMethods.evaluate(expr,
Jeva.extendEnv
(int.class, "price", new Integer(pr),
Jeva.extendEnv
(double.class, "tax", new Double(tx),
defaultEnv)));
total += result.intValue();
}
}
}
catch (ParseException e) { throw new Error(""+e); }
catch (VerifyingException e) { throw new Error(""+e); }
catch (ThrowException e) { throw new Error(""+e); }
return total;
}
public static void main(String[] args) {
int nExprs = 100;
int nEvals = 10;
System.out.println
("pre-running to load classes and invoke JIT on Jeva classes");
runByReparsingForEachEval(10, 30);
runByMakingEnvForEachEval(10, 30);
System.out.println("...done");
System.out.println("--- Timed runs ---");
timeRunByReparsingForEachEval(nExprs, nEvals);
timeRunByMakingEnvForEachEval(nExprs, nEvals);
System.out.println("--- Repeat to check for consistency between runs ---");
timeRunByReparsingForEachEval(nExprs, nEvals);
timeRunByMakingEnvForEachEval(nExprs, nEvals);
System.out.println("--- Check for consistency between methods ---");
timeRunByMakingEnvForEachEval(nExprs*nEvals, 1);
System.out.println("--- Vary to show marginal costs: Reparsing ---");
timeRunByReparsingForEachEval(nExprs, nEvals);
timeRunByReparsingForEachEval(2*nExprs, nEvals);
timeRunByReparsingForEachEval(3*nExprs, nEvals);
System.out.println("--- Vary to show marginal costs: Making Envs ---");
System.out.println("Vary nExprs:");
timeRunByMakingEnvForEachEval(nExprs, nEvals);
timeRunByMakingEnvForEachEval(2*nExprs, nEvals);
timeRunByMakingEnvForEachEval(3*nExprs, nEvals);
System.out.println("Vary nEvals:");
timeRunByMakingEnvForEachEval(nExprs, nEvals);
timeRunByMakingEnvForEachEval(nExprs, 2*nEvals);
timeRunByMakingEnvForEachEval(nExprs, 3*nEvals);
}
static final void timeRunByReparsingForEachEval(int nExprs, int nEvals) {
System.out.println("Parse and eval "+nExprs+"*"+nEvals+" expressions:");
System.gc(); long startTime = System.currentTimeMillis();
int result = runByReparsingForEachEval(nExprs, nEvals);
long duration = System.currentTimeMillis() - startTime;
System.out.println("Result: "+result+", duration: "+duration/1000.0+"sec");
}
static final void timeRunByMakingEnvForEachEval(int nExprs, int nEvals) {
System.out.println
("Parse "+nExprs+", eval each "+nEvals+" times in different envs:");
System.gc(); long startTime = System.currentTimeMillis();
int result = runByMakingEnvForEachEval(nExprs, nEvals);
long duration = System.currentTimeMillis() - startTime;
System.out.println("Result: "+result+", duration: "+duration/1000.0+"sec");
}
}