Skip to content

Dlist performance #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmarks/build.gradle → performance/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
configureAllRetroLambda()

dependencies {
compile project(":core")
testCompile dependencyJunit
}
97 changes: 97 additions & 0 deletions performance/src/main/java/fj/data/DListPerformance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package fj.data;

/**
* Difference List performance benchmarks comparing DList to Seq and List
* over 10 runs for the methods range, append and unbox.
*
* @author clintonselke
*/
public class DListPerformance {

static final int TOTAL_RUNS = 10;

private interface BenchmarkMethods<C> {
C range(int from, int to);
C append(C a, C b);
List<Integer> unbox(C a);
}

private static final BenchmarkMethods<List<Integer>> listMethods = new BenchmarkMethods<List<Integer>>() {
@Override
public List<Integer> range(int from, int to) {
return List.range(from, to);
}
@Override
public List<Integer> append(List<Integer> a, List<Integer> b) {
return a.append(b);
}
@Override
public List<Integer> unbox(List<Integer> a) {
return a;
}
};

private static final BenchmarkMethods<Seq<Integer>> seqMethods = new BenchmarkMethods<Seq<Integer>>() {
@Override
public Seq<Integer> range(int from, int to) {
return Seq.fromList(List.range(from, to));
}
@Override
public Seq<Integer> append(Seq<Integer> a, Seq<Integer> b) {
return a.append(b);
}
@Override
public List<Integer> unbox(Seq<Integer> a) {
return a.toList();
}
};

private static final BenchmarkMethods<DList<Integer>> dListMethods = new BenchmarkMethods<DList<Integer>>() {
@Override
public DList<Integer> range(int from, int to) {
return DList.fromList(List.range(from, to));
}
@Override
public DList<Integer> append(DList<Integer> a, DList<Integer> b) {
return a.append(b);
}
@Override
public List<Integer> unbox(DList<Integer> a) {
return a.run();
}
};

private static <C> double benchmark(BenchmarkMethods<C> methods) {
long msStart = System.currentTimeMillis();

for (int runNumber = 0; runNumber < TOTAL_RUNS; ++runNumber) {
final C xs = methods.range(0, 100);
C r = xs;
for (int i = 1; i < 2000; ++i) {
r = methods.append(r, xs);
}
List<Integer> r2 = methods.unbox(r);
for (Integer x : r2) {}
}
long msEnd = System.currentTimeMillis();
return (msEnd - msStart) / ((double) TOTAL_RUNS);
}

public static void main(String[] params) {
System.out.println("Starting difference list (DList) performance benchmark...");
// warm up
System.out.println("warm up...");
benchmark(listMethods);
benchmark(seqMethods);
benchmark(dListMethods);
// actual run
System.out.println("running benchmark...");
double listTime = benchmark(listMethods);
double seqTime = benchmark(seqMethods);
double dListTime = benchmark(dListMethods);
System.out.println("Average over " + TOTAL_RUNS + " runs...");
System.out.println("List: " + listTime + "ms");
System.out.println("Seq: " + seqTime + "ms");
System.out.println("DList: " + dListTime + "ms");
}
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

rootProject.name = "functionaljava"

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