(* example showing the use of difference lists, and SML timers *) (* the vanilla-flavor reverse1, which uses the append operator *) fun reverse1([]) = [] | reverse1(x::xs) = reverse1(xs)@[x]; (* the not so obvious, but maybe faster version *) fun auxReverse(nil, M) = M | auxReverse(x::xs, ys) = auxReverse(xs, x::ys); fun reverse2([]) = [] | reverse2(x::xs) = auxReverse(x::xs, nil); (* test them a little *) val l1 = [3,1,4,1,5,9,2,6,5,3]; reverse1(l1) = reverse2(l1); (* a funtion to run them many times *) fun runIt(F: int list -> int list, n:int, maxn:int) = if n=maxn then F l1 else (runIt(F, n + 1, maxn); F l1); (* and a function to time them *) fun clockIt(F: int list -> int list, n:int, maxn:int) = let val t1 = Timer.startCPUTimer(); in (runIt(F, n, maxn); let val recTimer = Timer.checkCPUTimer(t1); val t1gc = Time.toString(#gc(recTimer)); val t1sys = Time.toString(#sys(recTimer)); val t1usr = Time.toString(#usr(recTimer)); in print("gc is "^t1gc^" sys is "^t1sys^" usr is "^t1usr^"\n") end) end; SMLofNJ.Internals.GC.messages(false); print("Timing of reverse1\n"); clockIt(reverse1,1,3000000); print("Timing of reverse2\n"); clockIt(reverse2,1,3000000);