Debian Codegraph @

benchmarks/rnrs-benchmarks/perm9.ss

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 (library (rnrs-benchmarks perm9)
37 (export main)
38 (import (rnrs) (rnrs r5rs) (rnrs-benchmarks))
39
40 ; Procedure P_n generates a grey code of all perms of n elements
41 ; on top of stack ending with reversal of starting sequence
42 ;
43 ; F_n is flip of top n elements.
44 ;
45 ;
46 ; procedure P_n
47 ;
48 ; if n>1 then
49 ; begin
50 ; repeat P_{n-1},F_n n-1 times;
51 ; P_{n-1}
52 ; end
53 ;
54
55 (define (permutations x)
56 (let ((x x)
57 (perms (list x)))
58 (define (P n)
59 (if (> n 1)
60 (do ((j (- n 1) (- j 1)))
61 ((zero? j)
62 (P (- n 1)))
63 (P (- n 1))
64 (F n))))
65 (define (F n)
66 (set! x (revloop x n (list-tail x n)))
67 (set! perms (cons x perms)))
68 (define (revloop x n y)
69 (if (zero? n)
70 y
71 (revloop (cdr x)
72 (- n 1)
73 (cons (car x) y))))
74 (define (list-tail x n)
75 (if (zero? n)
76 x
77 (list-tail (cdr x) (- n 1))))
78 (P (length x))
79 perms))
80
81 ; Given a list of lists of numbers, returns the sum of the sums
82 ; of those lists.
83 ;
84 ; for (; x != NULL; x = x->rest)
85 ; for (y = x->first; y != NULL; y = y->rest)
86 ; sum = sum + y->first;
87
88 (define (sumlists x)
89 (do ((x x (cdr x))
90 (sum 0 (do ((y (car x) (cdr y))
91 (sum sum (+ sum (car y))))
92 ((null? y) sum))))
93 ((null? x) sum)))
94
95 (define (one..n n)
96 (do ((n n (- n 1))
97 (p '() (cons n p)))
98 ((zero? n) p)))
99
100 (define (main . args)
101 (let ((n 9))
102 (define (factorial n)
103 (if (zero? n)
104 1
105 (* n (factorial (- n 1)))))
106 (run-benchmark
107 (string-append "perm" (number->string n))
108 perm9-iters
109 (lambda (result)
110 (= (sumlists result)
111 (* (quotient (* n (+ n 1)) 2) (factorial n))))
112 (lambda (lst)
113 (lambda ()
114 (permutations lst)))
115 (one..n n)))))