1
|
public class LambdaPerf
|
2
|
{
|
3
|
public static void main(String[] args)
|
4
|
throws Exception
|
5
|
{
|
6
|
LambdaPerf p = new LambdaPerf();
|
7
|
|
8
|
{
|
9
|
long n1 = System.nanoTime();
|
10
|
for (int i = 0; i < 18_000_000; i++)
|
11
|
{
|
12
|
p.m1(false);
|
13
|
}
|
14
|
long n2 = System.nanoTime();
|
15
|
System.out.println("Lambda call: " + (n2 - n1) / 1_000_000d);
|
16
|
}
|
17
|
|
18
|
{
|
19
|
long n1 = System.nanoTime();
|
20
|
for (int i = 0; i < 18_000_000; i++)
|
21
|
{
|
22
|
p.m1b(false);
|
23
|
}
|
24
|
long n2 = System.nanoTime();
|
25
|
System.out.println("Runnable call: " + (n2 - n1) / 1_000_000d);
|
26
|
}
|
27
|
|
28
|
{
|
29
|
long n1 = System.nanoTime();
|
30
|
for (int i = 0; i < 18_000_000; i++)
|
31
|
{
|
32
|
p.getImpl(false);
|
33
|
}
|
34
|
long n2 = System.nanoTime();
|
35
|
System.out.println("Direct call: " + (n2 - n1) / 1_000_000d);
|
36
|
}
|
37
|
|
38
|
{
|
39
|
long n1 = System.nanoTime();
|
40
|
for (int i = 0; i < 18_000_000; i++)
|
41
|
{
|
42
|
try
|
43
|
{
|
44
|
p.getImpl2(false);
|
45
|
}
|
46
|
catch (Exception e)
|
47
|
{
|
48
|
|
49
|
}
|
50
|
}
|
51
|
long n2 = System.nanoTime();
|
52
|
System.out.println("Direct call with throw: " + (n2 - n1) / 1_000_000d);
|
53
|
}
|
54
|
}
|
55
|
|
56
|
public void m1(boolean create)
|
57
|
{
|
58
|
Object[] res = new Object[1];
|
59
|
timer((Operation) () -> {
|
60
|
res[0] = getImpl(create);
|
61
|
});
|
62
|
}
|
63
|
|
64
|
public void m1b(boolean create)
|
65
|
{
|
66
|
Object[] res = new Object[1];
|
67
|
timer(new Runnable() { public void run() {
|
68
|
res[0] = getImpl(create);
|
69
|
}
|
70
|
});
|
71
|
}
|
72
|
|
73
|
private Boolean getImpl(boolean create)
|
74
|
{
|
75
|
return null;
|
76
|
}
|
77
|
|
78
|
private Boolean getImpl2(boolean create)
|
79
|
throws Exception
|
80
|
{
|
81
|
return null;
|
82
|
}
|
83
|
|
84
|
private void timer(Operation op)
|
85
|
{
|
86
|
try
|
87
|
{
|
88
|
op.exec();
|
89
|
}
|
90
|
catch (Exception e)
|
91
|
{
|
92
|
e.printStackTrace();
|
93
|
}
|
94
|
}
|
95
|
|
96
|
private void timer(Runnable r)
|
97
|
{
|
98
|
r.run();
|
99
|
}
|
100
|
|
101
|
@FunctionalInterface
|
102
|
public static interface Operation
|
103
|
{
|
104
|
public void exec()
|
105
|
throws Exception;
|
106
|
}
|
107
|
}
|
108
|
|