Created
June 11, 2010 07:23
-
-
Save kfl/434166 to your computer and use it in GitHub Desktop.
Exercise the garbage collector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun genVec n = Vector.tabulate(n, fn i => i) | |
local val gen = Random.newgenseed 42.0 in | |
fun random_length () = Random.range(500, 200*16192) gen | |
fun range rng = let val f = Random.range rng in fn () => f gen end | |
end | |
fun initial n = List.tabulate(n, fn _ => genVec(random_length())) | |
fun split_at i lst = | |
let fun loop 0 ahead after = (rev ahead, after) | |
| loop n ahead (x::xs) = loop (n-1) (x::ahead) xs | |
| loop _ _ _ = raise Fail "Cannot happen" | |
in loop i [] lst end | |
fun doit n m = | |
let val rand_elem = range (0, n-1) | |
val zero = Vector.tabulate(100, fn _ => 0) | |
fun loop 0 _ = () | |
| loop j lst = | |
let val _ = lst@[zero] = lst | |
val i = rand_elem() | |
val (ahead, after) = split_at i lst | |
in print ("looping "^Int.toString j^"\n") | |
; loop (j-1) (ahead @ (genVec(random_length())) :: List.tl after) | |
end | |
in loop m (initial n) end | |
val main : unit = doit 100 50; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment