KickJava   Java API By Example, From Geeks To Geeks.

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


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.matrixJep.*;
12 import org.lsmp.djep.vectorJep.*;
13
14 /**
15  * Compares the speed of matrix operations
16  * using both VectorJep or MatrixJep.
17  */

18 public class MatrixSpeed {
19     static MatrixJep mj;
20     static VectorJep vj;
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         initVec();
27         long t2 = System.currentTimeMillis();
28         System.out.println("Vec initialise "+(t2-t1));
29         initMat();
30         long t3 = System.currentTimeMillis();
31         System.out.println("Mat initialise "+(t3-t2));
32         
33         doBoth("y=[[1,2,3],[3,4,5],[6,7,8]]","y*y");
34         doBoth("y=[[1,2,3],[3,4,5],[6,7,8]]","y+y");
35         doBoth("y=[[1,2,3],[3,4,5],[6,7,8]]","y-y");
36         doBoth("y=[[1,2,3],[3,4,5],[6,7,8]]","y*y+y");
37         doBoth("y=[1,2,3]","y+y");
38         doBoth("y=[1,2,3]","y . y");
39         doBoth("y=[1,2,3]","y^^y");
40     }
41     
42     public static void doBoth(String JavaDoc eqn1,String JavaDoc eqn2)
43     {
44         System.out.println("Testing speed for <"+eqn1+"> and <"+eqn2+">");
45         doVec(eqn1,eqn2);
46         doMat(eqn1,eqn2);
47     }
48     static void initVec()
49     {
50         vj = new VectorJep();
51         vj.addStandardConstants();
52         vj.addStandardFunctions();
53         vj.addComplex();
54         vj.setAllowUndeclared(true);
55         vj.setImplicitMul(true);
56         vj.setAllowAssignment(true);
57     }
58     
59     static void doVec(String JavaDoc eqn1,String JavaDoc eqn2)
60     {
61     // System.out.println("vec init"+(t4-t3));
62
try
63         {
64             Node node1 = vj.parse(eqn1);
65             vj.evaluate(node1);
66             Node node = vj.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                 vj.evaluate(node);
71             long t2 = System.currentTimeMillis();
72             System.out.println("Using VectorJep :"+(t2-t1));
73         }
74         catch(Exception JavaDoc e) {System.out.println("Error"+e.getMessage());}
75     }
76     
77     static void initMat()
78     {
79         mj = new MatrixJep();
80         mj.addStandardConstants();
81         mj.addStandardFunctions();
82         mj.addComplex();
83         mj.setAllowUndeclared(true);
84         mj.setImplicitMul(true);
85         mj.setAllowAssignment(true);
86     }
87     
88     static void doMat(String JavaDoc eqn1, String JavaDoc eqn2)
89     {
90         try
91         {
92             Node node2 = mj.simplify(mj.preprocess(mj.parse(eqn1)));
93             mj.evaluate(node2);
94             Node node3 = mj.simplify(mj.preprocess(mj.parse(eqn2)));
95             long t1 = System.currentTimeMillis();
96     // System.out.println("mat parse"+(t1-t4));
97
for(int i=0;i<num_itts;++i)
98                 mj.evaluateRaw(node3);
99             long t2 = System.currentTimeMillis();
100             System.out.println("Using MatrixJep :"+(t2-t1));
101         }
102         catch(Exception JavaDoc e) {System.out.println("Error"+e.getMessage());}
103     }
104 }
105
Popular Tags