Debian Codegraph @

benchmarks.larceny/src/perm9.scm

raw
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ; File: perm9.sch
3 ; Description: memory system benchmark using Zaks's permutation generator
4 ; Author: Lars Hansen, Will Clinger, and Gene Luks
5 ; Created: 18-Mar-94
6 ; Language: Scheme
7 ; Status: Public Domain
8 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9
10 ; 940720 / lth Added some more benchmarks for the thesis paper.
11 ; 970215 / wdc Increased problem size from 8 to 9; improved tenperm9-benchmark.
12 ; 970531 / wdc Cleaned up for public release.
13 ; 981116 / wdc Simplified to fit in with Feeley's benchmark suite.
14
15 ; The perm9 benchmark generates a list of all 362880 permutations of
16 ; the first 9 integers, allocating 1349288 pairs (typically 10,794,304
17 ; bytes), all of which goes into the generated list. (That is, the
18 ; perm9 benchmark generates absolutely no garbage.) This represents
19 ; a savings of about 63% over the storage that would be required by
20 ; an unshared list of permutations. The generated permutations are
21 ; in order of a grey code that bears no obvious relationship to a
22 ; lexicographic order.
23 ;
24 ; The 10perm9 benchmark repeats the perm9 benchmark 10 times, so it
25 ; allocates and reclaims 13492880 pairs (typically 107,943,040 bytes).
26 ; The live storage peaks at twice the storage that is allocated by the
27 ; perm9 benchmark. At the end of each iteration, the oldest half of
28 ; the live storage becomes garbage. Object lifetimes are distributed
29 ; uniformly between 10.3 and 20.6 megabytes.
30
31 ; Date: Thu, 17 Mar 94 19:43:32 -0800
32 ; From: [email protected]
33 ; To: will
34 ; Subject: Pancake flips
35 ;
36 ; Procedure P_n generates a grey code of all perms of n elements
37 ; on top of stack ending with reversal of starting sequence
38 ;
39 ; F_n is flip of top n elements.
40 ;
41 ;
42 ; procedure P_n
43 ;
44 ; if n>1 then
45 ; begin
46 ; repeat P_{n-1},F_n n-1 times;
47 ; P_{n-1}
48 ; end
49 ;
50
51 (define (permutations x)
52 (let ((x x)
53 (perms (list x)))
54 (define (P n)
55 (if (> n 1)
56 (do ((j (- n 1) (- j 1)))
57 ((zero? j)
58 (P (- n 1)))
59 (P (- n 1))
60 (F n))))
61 (define (F n)
62 (set! x (revloop x n (list-tail x n)))
63 (set! perms (cons x perms)))
64 (define (revloop x n y)
65 (if (zero? n)
66 y
67 (revloop (cdr x)
68 (- n 1)
69 (cons (car x) y))))
70 (define (list-tail x n)
71 (if (zero? n)
72 x
73 (list-tail (cdr x) (- n 1))))
74 (P (length x))
75 perms))
76
77 ; Given a list of lists of numbers, returns the sum of the sums
78 ; of those lists.
79 ;
80 ; for (; x != NULL; x = x->rest)
81 ; for (y = x->first; y != NULL; y = y->rest)
82 ; sum = sum + y->first;
83
84 (define (sumlists x)
85 (do ((x x (cdr x))
86 (sum 0 (do ((y (car x) (cdr y))
87 (sum sum (+ sum (car y))))
88 ((null? y) sum))))
89 ((null? x) sum)))
90
91 (define (one..n n)
92 (do ((n n (- n 1))
93 (p '() (cons n p)))
94 ((zero? n) p)))
95
96 (define (main . args)
97 (let ((n 9))
98 (define (factorial n)
99 (if (zero? n)
100 1
101 (* n (factorial (- n 1)))))
102 (run-benchmark
103 (string-append "perm" (number->string n))
104 perm9-iters
105 (lambda (result)
106 (= (sumlists result)
107 (* (quotient (* n (+ n 1)) 2) (factorial n))))
108 (lambda (lst)
109 (lambda ()
110 (permutations lst)))
111 (one..n n))))