1 8 9 package org.lsmp.djepExamples; 10 import org.nfunk.jep.*; 11 import org.lsmp.djep.rpe.*; 12 19 public class RpSpeed { 20 static JEP j; 21 static int num_itts = 100000; 24 public static void main(String args[]) { 25 long t1 = System.currentTimeMillis(); 26 initJep(); 27 long t2 = System.currentTimeMillis(); 28 System.out.println("Jep initialise "+(t2-t1)); 29 30 doAll(new String [0],"1*2*3+4*5*6+7*8*9"); 31 doAll(new String []{"x1=1","x2=2","x3=3","x4=4","x5=5","x6=6","x7=7","x8=8","x9=9"}, 32 "x1*x2*x3+x4*x5*x6+x7*x8*x9"); 33 doAll(new String []{"x=0.7"},"cos(x)^2+sin(x)^2"); 34 } 35 36 public static void doAll(String eqns[],String eqn2) 37 { 38 System.out.print("Testing speed for <"); 39 for(int i=0;i<eqns.length;++i) System.out.print(eqns[i]+","); 40 System.out.println("> and <"+eqn2+">"); 41 doJep(eqns,eqn2); 42 doRpe(eqns,eqn2); 43 System.out.println(); 44 } 45 46 static void initJep() 47 { 48 j = new JEP(); 49 j.addStandardConstants(); 50 j.addStandardFunctions(); 51 j.addComplex(); 52 j.setAllowUndeclared(true); 53 j.setImplicitMul(true); 54 j.setAllowAssignment(true); 55 } 56 57 static void doJep(String eqns[],String eqn2) 58 { 59 try 61 { 62 for(int i=0;i<eqns.length;++i) { 63 Node node2 = j.parse(eqns[i]); 64 j.evaluate(node2); 65 } 66 Node node = j.parse(eqn2); 67 long t1 = System.currentTimeMillis(); 68 for(int i=0;i<num_itts;++i) 70 j.evaluate(node); 71 long t2 = System.currentTimeMillis(); 72 System.out.println("Using Jep:\t"+(t2-t1)); 73 } 74 catch(Exception e) {System.out.println("Error"+e.getMessage());} 75 } 76 77 static void doRpe(String eqns[], String eqn2) 78 { 79 try 80 { 81 for(int i=0;i<eqns.length;++i) { 82 Node node2 = j.parse(eqns[i]); 83 j.evaluate(node2); 84 } 85 Node node3 = j.parse(eqn2); 86 RpEval rpe = new RpEval(j); 87 RpCommandList list = rpe.compile(node3); 88 long t1 = System.currentTimeMillis(); 89 for(int i=0;i<num_itts;++i) 91 rpe.evaluate(list); 92 long t2 = System.currentTimeMillis(); 93 System.out.print("Using RpEval2:\t\t"+(t2-t1)); 94 double res = rpe.evaluate(list); 95 System.out.println("\t"+res); 96 rpe.cleanUp(); 97 } 98 catch(Exception e) {System.out.println("Error"+e.getMessage());e.printStackTrace();} 99 } 100 } 101 | Popular Tags |