|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package main; |
| 7 | + |
| 8 | +import fj.data.DList; |
| 9 | +import fj.data.List; |
| 10 | +import fj.data.Seq; |
| 11 | +import fj.data.Stream; |
| 12 | + |
| 13 | +/** |
| 14 | + * |
| 15 | + * @author clintonselke |
| 16 | + */ |
| 17 | +public class Main { |
| 18 | + |
| 19 | + private interface BenchmarkMethods<C> { |
| 20 | + C range(int from, int to); |
| 21 | + C append(C a, C b); |
| 22 | + List<Integer> unbox(C a); |
| 23 | + } |
| 24 | + |
| 25 | + private static final BenchmarkMethods<List<Integer>> listMethods = new BenchmarkMethods<List<Integer>>() { |
| 26 | + @Override |
| 27 | + public List<Integer> range(int from, int to) { |
| 28 | + return List.range(from, to); |
| 29 | + } |
| 30 | + @Override |
| 31 | + public List<Integer> append(List<Integer> a, List<Integer> b) { |
| 32 | + return a.append(b); |
| 33 | + } |
| 34 | + @Override |
| 35 | + public List<Integer> unbox(List<Integer> a) { |
| 36 | + return a; |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + private static final BenchmarkMethods<Seq<Integer>> seqMethods = new BenchmarkMethods<Seq<Integer>>() { |
| 41 | + @Override |
| 42 | + public Seq<Integer> range(int from, int to) { |
| 43 | + return Seq.seq(List.range(from, to)); |
| 44 | + } |
| 45 | + @Override |
| 46 | + public Seq<Integer> append(Seq<Integer> a, Seq<Integer> b) { |
| 47 | + return a.append(b); |
| 48 | + } |
| 49 | + @Override |
| 50 | + public List<Integer> unbox(Seq<Integer> a) { |
| 51 | + return a.toList(); |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + private static final BenchmarkMethods<DList<Integer>> dListMethods = new BenchmarkMethods<DList<Integer>>() { |
| 56 | + @Override |
| 57 | + public DList<Integer> range(int from, int to) { |
| 58 | + return DList.fromList(List.range(from, to)); |
| 59 | + } |
| 60 | + @Override |
| 61 | + public DList<Integer> append(DList<Integer> a, DList<Integer> b) { |
| 62 | + return a.append(b); |
| 63 | + } |
| 64 | + @Override |
| 65 | + public List<Integer> unbox(DList<Integer> a) { |
| 66 | + return a.run(); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + private static <C> double benchmark(BenchmarkMethods<C> methods) { |
| 71 | + long msStart = System.currentTimeMillis(); |
| 72 | + for (int n = 0; n < 10; ++n) { |
| 73 | + final C xs = methods.range(0, 100); |
| 74 | + C r = xs; |
| 75 | + for (int i = 1; i < 2000; ++i) { |
| 76 | + r = methods.append(r, xs); |
| 77 | + } |
| 78 | + List<Integer> r2 = methods.unbox(r); |
| 79 | + for (Integer x : r2) {} |
| 80 | + } |
| 81 | + long msEnd = System.currentTimeMillis(); |
| 82 | + return (msEnd - msStart) / 10.0; |
| 83 | + } |
| 84 | + |
| 85 | + public static void main(String[] params) { |
| 86 | + // warm up |
| 87 | + benchmark(listMethods); |
| 88 | + benchmark(seqMethods); |
| 89 | + benchmark(dListMethods); |
| 90 | + // actual run |
| 91 | + double listTime = benchmark(listMethods); |
| 92 | + double seqTime = benchmark(seqMethods); |
| 93 | + double dListTime = benchmark(dListMethods); |
| 94 | + System.out.println("Average over 10 runs..."); |
| 95 | + System.out.println("List: " + listTime + "ms"); |
| 96 | + System.out.println("Seq: " + seqTime + "ms"); |
| 97 | + System.out.println("DList: " + dListTime + "ms"); |
| 98 | + } |
| 99 | +} |
0 commit comments