KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djepExamples > RpSpeed


1 /* @author rich
2  * Created on 26-Feb-2004
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8
9 package org.lsmp.djepExamples;
10 import org.nfunk.jep.*;
11 import org.lsmp.djep.rpe.*;
12 /**
13  * Compares the speed of matrix operations
14  * using both VectorJep or MatrixJep.
15  * <p>
16  * If you have some nice complicated examples, I'd love to
17  * hear about them to see if we can tune things up. - rich
18  */

19 public class RpSpeed {
20     static JEP j;
21     static int num_itts = 100000; // for normal use
22
// static int num_itts = 1000; // for use with profiler
23

24     public static void main(String JavaDoc 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 JavaDoc[0],"1*2*3+4*5*6+7*8*9");
31         doAll(new String JavaDoc[]{"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 JavaDoc[]{"x=0.7"},"cos(x)^2+sin(x)^2");
34     }
35     
36     public static void doAll(String JavaDoc eqns[],String JavaDoc 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 JavaDoc eqns[],String JavaDoc eqn2)
58     {
59     // System.out.println("vec init"+(t4-t3));
60
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     // System.out.println("vec parse"+(t1-t4));
69
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 JavaDoc e) {System.out.println("Error"+e.getMessage());}
75     }
76
77     static void doRpe(String JavaDoc eqns[], String JavaDoc 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     // System.out.println("mat parse"+(t1-t4));
90
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 JavaDoc e) {System.out.println("Error"+e.getMessage());e.printStackTrace();}
99     }
100 }
101
Popular Tags