Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 46f1784

Browse filesBrowse files
committed
add batch evaluate benchark sqrt and taylor
1 parent 82feba9 commit 46f1784
Copy full SHA for 46f1784

File tree

Expand file treeCollapse file tree

2 files changed

+103
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+103
-1
lines changed
Open diff view settings
Collapse file

‎src/symjava/examples/BenchmarkSqrt.java‎

Copy file name to clipboardExpand all lines: src/symjava/examples/BenchmarkSqrt.java
+52-1Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
import java.util.ArrayList;
77

8+
import symjava.bytecode.BytecodeBatchFunc;
89
import symjava.bytecode.BytecodeFunc;
910
import symjava.symbolic.Expr;
1011
import symjava.symbolic.Func;
12+
import symjava.symbolic.utils.JIT;
1113

1214
public class BenchmarkSqrt {
1315
public static double factorial(int n) {
@@ -17,12 +19,17 @@ public static double factorial(int n) {
1719
return rlt;
1820
}
1921
public static void main(String[] args) {
22+
test();
23+
testBatchEval();
24+
}
25+
26+
public static void test() {
2027
int n = 9;
2128
Expr expr = 0;
2229

2330
Expr term;
2431
ArrayList<Expr> exprs = new ArrayList<Expr>();
25-
for(int i=1; i<=n; i++) {
32+
for(int i=1; i<(n+1); i++) {
2633
term = (pow(x, 1.0/i));
2734
//term = (sqrt(x, i));
2835
//System.out.println(term);
@@ -52,5 +59,49 @@ public static void main(String[] args) {
5259
}
5360
System.out.println("Test Value="+out);
5461
}
62+
63+
public static void testBatchEval() {
64+
int n = 9;
65+
Expr expr = 0;
66+
67+
Expr term;
68+
ArrayList<Expr> exprs = new ArrayList<Expr>();
69+
for(int i=1; i<(n+1); i++) {
70+
term = (pow(x, 1.0/i));
71+
//term = (sqrt(x, i));
72+
//System.out.println(term);
73+
expr = expr + term;
74+
exprs.add(expr);
75+
}
76+
77+
ArrayList<BytecodeBatchFunc> funcs = new ArrayList<BytecodeBatchFunc>();
78+
int batchLen = 10000;
79+
double[] outAry = new double[batchLen];
80+
double[] args = new double[batchLen];
5581

82+
for(int i=0; i<n; i++) {
83+
Func func = new Func("func"+i, exprs.get(i));
84+
BytecodeBatchFunc bfunc = JIT.compileBatchFunc(func.args(), func);
85+
funcs.add(bfunc);
86+
}
87+
88+
int N=10000000/batchLen;
89+
double out = 0.0;
90+
double x = 0.1;
91+
for(int i=0; i<funcs.size(); i++) {
92+
long begin = System.currentTimeMillis();
93+
for(int j=0; j<N; j++) {
94+
for(int k=0; k<batchLen; k++) {
95+
x += 1e-15;
96+
args[k] = x;
97+
}
98+
funcs.get(i).apply(outAry, 0, args);
99+
for(int k=0; k<batchLen; k++)
100+
out += outAry[k];
101+
}
102+
long end = System.currentTimeMillis();
103+
System.out.println("Time: "+((end-begin)/1000.0)+" expr="+exprs.get(i));
104+
}
105+
System.out.println("Test Value="+out);
106+
}
56107
}
Collapse file

‎src/symjava/examples/BenchmarkTaylor.java‎

Copy file name to clipboardExpand all lines: src/symjava/examples/BenchmarkTaylor.java
+51Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
import java.util.ArrayList;
77

8+
import symjava.bytecode.BytecodeBatchFunc;
89
import symjava.bytecode.BytecodeFunc;
910
import symjava.symbolic.Expr;
1011
import symjava.symbolic.Func;
12+
import symjava.symbolic.utils.JIT;
1113

1214
public class BenchmarkTaylor {
1315
public static double factorial(int n) {
@@ -17,6 +19,11 @@ public static double factorial(int n) {
1719
return rlt;
1820
}
1921
public static void main(String[] args) {
22+
test();
23+
testBatchEval();
24+
}
25+
26+
public static void test() {
2027
int n = 10;
2128
Expr expr = 0;
2229

@@ -51,5 +58,49 @@ public static void main(String[] args) {
5158
}
5259
System.out.println("Test Value="+out);
5360
}
61+
62+
public static void testBatchEval() {
63+
int n = 10;
64+
Expr expr = 0;
65+
66+
Expr term;
67+
ArrayList<Expr> exprs = new ArrayList<Expr>();
68+
for(int i=0; i<n; i++) {
69+
term = (1.0/factorial(i))*pow(x, i);
70+
//System.out.println(term);
71+
expr = expr + term;
72+
exprs.add(expr);
73+
}
74+
75+
ArrayList<BytecodeBatchFunc> funcs = new ArrayList<BytecodeBatchFunc>();
76+
int batchLen = 100000;
77+
double[] outAry = new double[batchLen];
78+
double[] args = new double[batchLen];
79+
80+
for(int i=0; i<n; i++) {
81+
Func func = new Func("func"+i, exprs.get(i));
82+
BytecodeBatchFunc bfunc = JIT.compileBatchFunc(func.args(), func);
83+
funcs.add(bfunc);
84+
}
85+
86+
int N=10000000/batchLen;
87+
double out = 0.0;
88+
double x = 0.1;
89+
for(int i=0; i<funcs.size(); i++) {
90+
long begin = System.currentTimeMillis();
91+
for(int j=0; j<N; j++) {
92+
for(int k=0; k<batchLen; k++) {
93+
x += 1e-15;
94+
args[k] = x;
95+
}
96+
funcs.get(i).apply(outAry, 0, args);
97+
for(int k=0; k<batchLen; k++)
98+
out += outAry[k];
99+
}
100+
long end = System.currentTimeMillis();
101+
System.out.println("Time: "+((end-begin)/1000.0)+" expr="+exprs.get(i));
102+
}
103+
System.out.println("Test Value="+out);
104+
}
54105

55106
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.