Debian Codegraph @

benchmarks.larceny/src/diviter.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 diviter {
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 int list_length (Pair l) {
41 int n = 0;
42 while (l != null) {
43 l = Pair.cdr(l);
44 n++;
45 }
46 return n;
47 }
48
49 /*===========================================================================*/
50
51 public static void main (String args[])
52 {
53 int i;
54 Pair result;
55 Pair ll;
56 ll = create_n (200);
57
58 result = iterative_div2 (ll);
59 for (i=1; i<400000; i++) {
60 result = iterative_div2 (ll);
61 }
62
63 if (list_length (result) != 100)
64 System.out.println ("*** wrong result ***");
65 }
66
67 }