Last active
August 29, 2015 14:20
-
-
Save KeenS/adba116713480c2e5d23 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
_require "basis.smi" | |
structure Benchmark = | |
struct | |
val repeat: int ->(unit -> 'a) -> unit | |
val bench: int -> (unit -> 'a) -> LargeInt.int | |
val benchmark: string -> int -> (unit -> 'a) -> unit | |
end |
This file contains hidden or 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
structure Benchmark = | |
struct | |
fun repeat 0 f = () | |
| repeat n f = (f ();repeat (n - 1) f) | |
fun bench n f = let | |
val startTime = Time.now () | |
val _ = repeat n f | |
val endTime = Time.now () | |
in | |
Time.toMilliseconds (Time.-(endTime, startTime)) | |
end | |
fun benchmark name n f = let | |
val time = bench n f | |
in | |
print (name ^ "\n"); | |
print (" Time:\n"); | |
print (" [Total] " ^ (LargeInt.toString time) ^ " ms/" ^ (Int.toString n) ^ "calls\n"); | |
print (" [Average] " ^ (Real.toString((Real.fromLargeInt time) / (Real.fromInt n))) ^ " ms/call\n") | |
end | |
fun nChars n char = CharArray.vector(CharArray.array(n, char)) | |
fun toWidth width str = let | |
val len = String.size str | |
in | |
if len < width | |
then str ^ (nChars (width - len) #" ") | |
else str | |
end | |
fun histLine width base value = | |
(nChars (Int.fromLarge(width * value div base)) #"*") ^ "\n" | |
fun benchset name n fs = let | |
val res = List.map (fn (label, f) => (label, bench n f)) fs | |
val max = List.foldl (fn ((_, time), m) => LargeInt.max(time, m)) 0 res | |
val maxLen = List.foldl (fn ((label, _), m) => Int.max(String.size label, m)) 0 fs | |
in | |
print "name:\n"; | |
print ((nChars ((String.size " ") + maxLen) #"-") ^ "+" ^ (nChars ((String.size "|") + 50) #"-") ^ "\n"); | |
app (fn (label, time) => print(" " ^ (toWidth maxLen label) ^ "|" ^(histLine (50:LargeInt.int) max time))) res; | |
print ((nChars ((String.size " ") + maxLen) #"-") ^ "+" ^ (nChars ((String.size "|") + 50) #"-") ^ "\n") | |
end | |
end | |
fun fib n = if n <= 1 then 1 else (fib (n - 1)) + (fib (n - 2)) | |
val _ = Benchmark.benchset "fib" 1 [("24" , (fn _ => fib 24)), ("32" , (fn _ => fib 32)), ("40" , (fn _ => fib 40))] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment