/
GuavaPerformanceTests.java
147 lines (123 loc) · 4.45 KB
/
GuavaPerformanceTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*******************************************************************************
* Copyright (c) 2012 Arieh 'Vainolo' Bibliowicz
* You can use this code for educational purposes. For any other uses
* please contact me: vainolo@gmail.com
*******************************************************************************/
package com.vainolo.examples.guava;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
public class GuavaPerformanceTests {
private Random rand = new Random();
private int numberOfIterations;
private int elementRange;
private int numberOfElements;
static int counter = 0;
public GuavaPerformanceTests(int numberOfIterations, int numberOfElements, int elementRange) {
this.numberOfIterations = numberOfIterations;
this.numberOfElements = numberOfElements;
this.elementRange = elementRange;
}
private Collection<Integer> initializeSource() {
List<Integer> source = new ArrayList<Integer>();
for(int i = 0; i < numberOfElements; i++) {
source.add(rand.nextInt(elementRange));
}
return source;
}
private Predicate<Integer> initializePredicate() {
final int range = rand.nextInt(elementRange);
Predicate<Integer> predicate = new Predicate<Integer>() {
@Override
public boolean apply(Integer input) {
return input < range;
}
};
return predicate;
}
private Collection<Integer> copyFilter(Collection<Integer> source, Predicate<Integer> p) {
ArrayList<Integer> retVal = new ArrayList<Integer>();
for(Integer integer : source) {
if(p.apply(integer))
retVal.add(integer);
}
return retVal;
}
private Collection<Integer> guavaFilter(Collection<Integer> source, Predicate<Integer> p) {
return Collections2.filter(source, p);
}
private TestResult run() {
TestResult result = new TestResult();
long start, end;
Collection<Integer> source = initializeSource(), target;
for(int i = 0; i < numberOfIterations; i++) {
Predicate<Integer> predicate = initializePredicate();
// Copy tests
start = System.currentTimeMillis();
target = copyFilter(source, predicate);
end = System.currentTimeMillis();
result.copyFilterTime += (end - start);
start = System.currentTimeMillis();
for(int j : target) {
counter += j;
}
end = System.currentTimeMillis();
result.copyAccessTime += (end - start);
start = System.currentTimeMillis();
for(int k = 0; k < 10; k++) {
for(int j : target) {
counter += j;
}
}
end = System.currentTimeMillis();
result.multipleCopyAccessTime += (end - start);
// Guava tests
start = System.currentTimeMillis();
target = guavaFilter(source, predicate);
end = System.currentTimeMillis();
result.guavaFilterTime += (end - start);
start = System.currentTimeMillis();
for(int j : target) {
counter += j;
}
end = System.currentTimeMillis();
result.guavaAccessTime += (end - start);
start = System.currentTimeMillis();
for(int k = 0; k < 10; k++) {
for(int j : target) {
counter += j;
}
}
end = System.currentTimeMillis();
result.multipleGuavaAccessTime += (end - start);
}
return result;
}
public static void main(String args[]) {
int[] numberOfElements = { 100, 1000, 10000 };
int[] elementRange = { 10, 100, 1000, 10000 };
System.out
.println("#Elements,Range,Copy Filter,Copy Access,Guava Filter,Guava Access,Multiple Copy Access, Multiple Guava Access");
for(int elements : numberOfElements) {
for(int range : elementRange) {
GuavaPerformanceTests tests = new GuavaPerformanceTests(1000, elements, range);
TestResult results = tests.run();
System.out.println("" + elements + "," + range + "," + results.copyFilterTime + "," + results.guavaFilterTime +
"," + results.copyAccessTime + "," + results.guavaAccessTime + "," + results.multipleCopyAccessTime + "," +
results.multipleGuavaAccessTime);
}
}
System.out.println("\n" + counter);
}
private class TestResult {
long copyFilterTime = 0;
long guavaFilterTime = 0;
long copyAccessTime = 0;
long guavaAccessTime = 0;
long multipleCopyAccessTime = 0;
long multipleGuavaAccessTime = 0;
}
}