Skip to content

Commit 7f7a8b3

Browse files
author
Sam Pullara
committed
performance in context not as good as the microbenchmark.
1 parent de6fb8d commit 7f7a8b3

File tree

3 files changed

+73
-94
lines changed

3 files changed

+73
-94
lines changed

indy/src/test/java/IndyDemo.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import java.lang.invoke.*;
2-
import java.lang.reflect.*;
1+
import com.github.mustachejava.indy.IndyWrapper;
2+
import com.github.mustachejava.reflect.ReflectionObjectHandler;
3+
import com.github.mustachejava.reflect.ReflectionWrapper;
4+
import com.github.mustachejava.util.Wrapper;
35

46
public class IndyDemo {
57
public static void main(String[] args) throws Throwable {
8+
IndyDemo indyDemo = new IndyDemo();
9+
REFLECTED = new ReflectionObjectHandler().find("someMethod", new Object[] { indyDemo });
10+
INDY = IndyWrapper.create((ReflectionWrapper) REFLECTED);
611
for (int i = 0; i < 10; i++) {
7-
timeReflection();
8-
timeIndy();
12+
timeReflection(indyDemo);
13+
timeIndy(indyDemo);
914
}
1015
}
1116

@@ -18,42 +23,30 @@ public static void main(String[] args) throws Throwable {
1823
}
1924
}
2025

21-
public static void timeReflection() throws Throwable {
26+
public static void timeReflection(IndyDemo indyDemo) throws Throwable {
2227
long start = System.currentTimeMillis();
28+
Object[] scopes = {indyDemo};
2329
for (int i = 0; i < 100000000; i++) {
24-
REFLECTED.invoke(null, strings[i % 100]);
30+
REFLECTED.call(scopes);
2531
}
2632
System.out.println("reflected: " + (System.currentTimeMillis() - start));
2733
}
2834

29-
public static void timeIndy() throws Throwable {
35+
public static void timeIndy(IndyDemo indyDemo) throws Throwable {
3036
long start = System.currentTimeMillis();
37+
Object[] scopes = {indyDemo};
3138
for (int i = 0; i < 100000000; i++) {
32-
HANDLE.invokeExact(strings[i % 100]);
39+
INDY.call(scopes);
3340
}
3441
System.out.println("indy: " + (System.currentTimeMillis() - start));
3542
}
3643

37-
private static final Method REFLECTED;
38-
private static final MethodHandle HANDLE;
44+
private static Wrapper REFLECTED;
45+
private static Wrapper INDY;
3946

40-
static {
41-
Method method = null;
42-
try {
43-
method = IndyDemo.class.getMethod("someMethod", String.class);
44-
} catch (Exception e) {}
45-
REFLECTED = method;
46-
47-
MethodHandle handle = null;
48-
try {
49-
handle = MethodHandles.lookup().unreflect(REFLECTED);
50-
} catch (Exception e) {}
51-
HANDLE = handle;
52-
}
53-
54-
public static int length = 0;
47+
private int length = 0;
5548

56-
public static void someMethod(String a) {
57-
length = a.length();
49+
public int someMethod() {
50+
return length++;
5851
}
5952
}

indy/src/test/java/com/github/mustachejava/ComplexObject.java

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,35 @@
33
import java.util.Arrays;
44
import java.util.List;
55

6-
/**
7-
* Created by IntelliJ IDEA.
8-
* User: spullara
9-
* Date: 1/17/12
10-
* Time: 8:17 PM
11-
* To change this template use File | Settings | File Templates.
12-
*/
136
public class ComplexObject {
14-
String header = "Colors";
15-
List<Color> item = Arrays.asList(
16-
new Color("red", true, "#Red"),
17-
new Color("green", false, "#Green"),
18-
new Color("blue", false, "#Blue")
19-
);
7+
String header = "Colors";
8+
List<Color> item = Arrays.asList(
9+
new Color("red", true, "#Red"),
10+
new Color("green", false, "#Green"),
11+
new Color("blue", false, "#Blue")
12+
);
2013

21-
boolean list() {
22-
return item.size() != 0;
23-
}
24-
25-
boolean empty() {
26-
return item.size() == 0;
27-
}
28-
29-
private static class Color {
30-
boolean link() {
31-
return !current;
14+
boolean list() {
15+
return item.size() != 0;
3216
}
3317

34-
Color(String name, boolean current, String url) {
35-
this.name = name;
36-
this.current = current;
37-
this.url = url;
18+
boolean empty() {
19+
return item.size() == 0;
3820
}
3921

40-
String name;
41-
boolean current;
42-
String url;
43-
}
22+
private static class Color {
23+
boolean link() {
24+
return !current;
25+
}
26+
27+
Color(String name, boolean current, String url) {
28+
this.name = name;
29+
this.current = current;
30+
this.url = url;
31+
}
32+
33+
String name;
34+
boolean current;
35+
String url;
36+
}
4437
}

indy/src/test/java/com/github/mustachejava/ParallelComplexObject.java

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,39 @@
44
import java.util.List;
55
import java.util.concurrent.Callable;
66

7-
/**
8-
* Created by IntelliJ IDEA.
9-
* User: spullara
10-
* Date: 1/17/12
11-
* Time: 8:19 PM
12-
* To change this template use File | Settings | File Templates.
13-
*/
147
public class ParallelComplexObject {
15-
String header = "Colors";
16-
Callable<List<Color>> item = new Callable<List<Color>>() {
17-
@Override
18-
public List<Color> call() throws Exception {
19-
return Arrays.asList(
20-
new Color("red", true, "#Red"),
21-
new Color("green", false, "#Green"),
22-
new Color("blue", false, "#Blue"));
8+
String header = "Colors";
9+
Callable<List<Color>> item = new Callable<List<Color>>() {
10+
@Override
11+
public List<Color> call() throws Exception {
12+
return Arrays.asList(
13+
new Color("red", true, "#Red"),
14+
new Color("green", false, "#Green"),
15+
new Color("blue", false, "#Blue"));
16+
}
17+
};
18+
19+
boolean list() {
20+
return true;
2321
}
24-
};
2522

26-
boolean list() {
27-
return true;
28-
}
23+
boolean empty() {
24+
return false;
25+
}
2926

30-
boolean empty() {
31-
return false;
32-
}
27+
private static class Color {
28+
boolean link() {
29+
return !current;
30+
}
3331

34-
private static class Color {
35-
boolean link() {
36-
return !current;
37-
}
32+
Color(String name, boolean current, String url) {
33+
this.name = name;
34+
this.current = current;
35+
this.url = url;
36+
}
3837

39-
Color(String name, boolean current, String url) {
40-
this.name = name;
41-
this.current = current;
42-
this.url = url;
38+
String name;
39+
boolean current;
40+
String url;
4341
}
44-
45-
String name;
46-
boolean current;
47-
String url;
48-
}
4942
}

0 commit comments

Comments
 (0)