Skip to content

Commit d66b8f5

Browse files
committed
Merge pull request functionaljava#225 from mperry/dlist-performance
Dlist performance
2 parents 0aef8c2 + b8fa525 commit d66b8f5

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-1
lines changed

benchmarks/build.gradle renamed to performance/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
configureAllRetroLambda()
33

44
dependencies {
5+
compile project(":core")
56
testCompile dependencyJunit
67
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package fj.data;
2+
3+
/**
4+
* Difference List performance benchmarks comparing DList to Seq and List
5+
* over 10 runs for the methods range, append and unbox.
6+
*
7+
* @author clintonselke
8+
*/
9+
public class DListPerformance {
10+
11+
static final int TOTAL_RUNS = 10;
12+
13+
private interface BenchmarkMethods<C> {
14+
C range(int from, int to);
15+
C append(C a, C b);
16+
List<Integer> unbox(C a);
17+
}
18+
19+
private static final BenchmarkMethods<List<Integer>> listMethods = new BenchmarkMethods<List<Integer>>() {
20+
@Override
21+
public List<Integer> range(int from, int to) {
22+
return List.range(from, to);
23+
}
24+
@Override
25+
public List<Integer> append(List<Integer> a, List<Integer> b) {
26+
return a.append(b);
27+
}
28+
@Override
29+
public List<Integer> unbox(List<Integer> a) {
30+
return a;
31+
}
32+
};
33+
34+
private static final BenchmarkMethods<Seq<Integer>> seqMethods = new BenchmarkMethods<Seq<Integer>>() {
35+
@Override
36+
public Seq<Integer> range(int from, int to) {
37+
return Seq.fromList(List.range(from, to));
38+
}
39+
@Override
40+
public Seq<Integer> append(Seq<Integer> a, Seq<Integer> b) {
41+
return a.append(b);
42+
}
43+
@Override
44+
public List<Integer> unbox(Seq<Integer> a) {
45+
return a.toList();
46+
}
47+
};
48+
49+
private static final BenchmarkMethods<DList<Integer>> dListMethods = new BenchmarkMethods<DList<Integer>>() {
50+
@Override
51+
public DList<Integer> range(int from, int to) {
52+
return DList.fromList(List.range(from, to));
53+
}
54+
@Override
55+
public DList<Integer> append(DList<Integer> a, DList<Integer> b) {
56+
return a.append(b);
57+
}
58+
@Override
59+
public List<Integer> unbox(DList<Integer> a) {
60+
return a.run();
61+
}
62+
};
63+
64+
private static <C> double benchmark(BenchmarkMethods<C> methods) {
65+
long msStart = System.currentTimeMillis();
66+
67+
for (int runNumber = 0; runNumber < TOTAL_RUNS; ++runNumber) {
68+
final C xs = methods.range(0, 100);
69+
C r = xs;
70+
for (int i = 1; i < 2000; ++i) {
71+
r = methods.append(r, xs);
72+
}
73+
List<Integer> r2 = methods.unbox(r);
74+
for (Integer x : r2) {}
75+
}
76+
long msEnd = System.currentTimeMillis();
77+
return (msEnd - msStart) / ((double) TOTAL_RUNS);
78+
}
79+
80+
public static void main(String[] params) {
81+
System.out.println("Starting difference list (DList) performance benchmark...");
82+
// warm up
83+
System.out.println("warm up...");
84+
benchmark(listMethods);
85+
benchmark(seqMethods);
86+
benchmark(dListMethods);
87+
// actual run
88+
System.out.println("running benchmark...");
89+
double listTime = benchmark(listMethods);
90+
double seqTime = benchmark(seqMethods);
91+
double dListTime = benchmark(dListMethods);
92+
System.out.println("Average over " + TOTAL_RUNS + " runs...");
93+
System.out.println("List: " + listTime + "ms");
94+
System.out.println("Seq: " + seqTime + "ms");
95+
System.out.println("DList: " + dListTime + "ms");
96+
}
97+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

22
rootProject.name = "functionaljava"
33

4-
include "core", "demo", "consume", "java8", "quickcheck", "props-core", "props-core-scalacheck", "java-core", "benchmarks"
4+
include "core", "demo", "consume", "java8", "quickcheck", "props-core", "props-core-scalacheck", "java-core", "performance"
55

0 commit comments

Comments
 (0)