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