Debian Codegraph @

benchmarks.larceny/src/divrec.java

raw
1 // DIVITER -- Benchmark which divides by 2 using lists of n ()'s.
2
3 class Pair {
4
5 // A Java compiler ought to generate inline code for these.
6
7 public static Pair cons (int n, Pair y) { return new Pair(n, y); }
8 public static int car (Pair x) { return x.hd; }
9 public static Pair cdr (Pair x) { return x.tl; }
10
11 // If it doesn't, then we'll inline them by hand.
12 // That's why the following are public.
13 // (But Sun's Java 1.2 does the inlining ok.)
14
15 public Pair (int n, Pair y) { hd = n; tl = y; }
16 public int hd;
17 public Pair tl;
18
19 }
20
21 class divrec {
22 static Pair ll;
23
24 static Pair create_n (int n) {
25 Pair result = null;
26 while (n > 0) {
27 n = n - 1;
28 result = Pair.cons ( 0, result );
29 }
30 return result;
31 }
32
33 static Pair iterative_div2 (Pair l) {
34 Pair a = null;
35 for (l = l; l != null; l = Pair.cdr (Pair.cdr (l)))
36 a = Pair.cons(Pair.car(l), a);
37 return a;
38 }
39
40 static Pair recursive_div2 (Pair l) {
41 if (l == null)
42 return null;
43 else {
44 Pair y = recursive_div2 (Pair.cdr(Pair.cdr( l )));
45 return Pair.cons ( Pair.car(l), y );
46 }
47 }
48
49 static int list_length (Pair l) {
50 int n = 0;
51 while (l != null) {
52 l = Pair.cdr(l);
53 n++;
54 }
55 return n;
56 }
57
58 /*===========================================================================*/
59
60 public static void main (String args[])
61 {
62 int i;
63 Pair result;
64 Pair ll;
65 ll = create_n (200);
66
67 result = recursive_div2 (ll);
68 for (i=1; i<400000; i++) {
69 result = recursive_div2 (ll);
70 }
71
72 if (list_length (result) != 100)
73 System.out.println ("*** wrong result ***");
74 }
75
76 }