Files
sml-projects/utils.sml

103 lines
2.6 KiB
Standard ML
Raw Normal View History

fun timeFun title f =
2023-11-13 05:17:27 +00:00
let
val title = String.concat ["Starting ", title, "..."]
2023-11-13 05:17:27 +00:00
val _ = (print title)
val startTime = Time.now ()
val startTime = Time.toNanoseconds startTime
val x = f ()
val endTime = Time.now ()
val endTime = Time.toNanoseconds endTime
val timeDiff = endTime - startTime
val timeDiff = LargeInt.toString timeDiff
val timeTook = String.concat ["took ", timeDiff, " nanoseconds\n"]
val _ = (print timeTook)
2023-11-13 05:17:27 +00:00
in
x
end
fun runTxns arr =
Vector.foldl
(fn ((pos, delNum, insStr), rope) =>
let
val rope = if delNum > 0 then Rope.delete (pos, delNum, rope) else rope
val strSize = String.size insStr
val rope =
if strSize > 0 then Rope.insert (pos, insStr, rope) else rope
in
rope
end) Rope.empty arr
2023-11-13 05:17:27 +00:00
fun runTxnsTime title arr =
let val f = (fn () => runTxns arr)
in timeFun title f
2023-11-13 05:17:27 +00:00
end
2023-11-13 06:05:36 +00:00
fun runToString rope = Rope.toString rope
2023-11-13 09:03:29 +00:00
fun runToStringTime title rope =
let val f = (fn () => runToString rope)
in timeFun title f
2023-11-13 09:03:29 +00:00
end
fun runTxns1000Times (counter, arr, total) =
2023-11-14 10:44:52 +00:00
if counter = 1000 then
2024-02-15 12:30:48 +00:00
let
val divisor = Int.toLarge 1000
val total = total div divisor
val str = LargeInt.toString total
in
print (str ^ "\n")
2024-02-15 12:30:48 +00:00
end
2023-11-14 10:44:52 +00:00
else
let
val startTime = Time.now ()
val startTime = Time.toNanoseconds startTime
2023-11-14 10:44:52 +00:00
val _ = runTxns arr
2023-11-14 10:44:52 +00:00
val endTime = Time.now ()
val endTime = Time.toNanoseconds endTime
val timeDiff = endTime - startTime
2024-02-15 12:30:48 +00:00
val counter = counter + 1
val total = timeDiff + total
2023-11-14 10:44:52 +00:00
in
runTxns1000Times (counter, arr, total)
2023-11-14 10:44:52 +00:00
end
fun writeFile filename acc =
let
2023-11-14 10:44:52 +00:00
val str = String.concatWith "," acc
val fd = TextIO.openOut filename
val _ = TextIO.output (fd, str) handle e => (TextIO.closeOut fd; raise e)
val _ = TextIO.closeOut fd
in
()
end
fun main () =
2023-11-13 06:05:36 +00:00
let
(* Timing benchmarks. *)
val startTime = LargeInt.fromInt 0
val _ = runTxns1000Times (999, svelte_arr, startTime)
val _ = runTxns1000Times (999, rust_arr, startTime)
val _ = runTxns1000Times (999, seph_arr, startTime)
val _ = runTxns1000Times (999, automerge_arr, startTime)
(* Tests that line metadata is correct; will fail if incorrect. *)
val svelte = runTxns svelte_arr
val _ = Rope.verifyLines svelte
2023-11-14 10:44:52 +00:00
val rust = runTxns rust_arr
val _ = Rope.verifyLines rust
2023-11-14 10:44:52 +00:00
val seph = runTxns seph_arr
val _ = Rope.verifyLines seph
2023-11-13 09:03:29 +00:00
val automerge = runTxns automerge_arr
val _ = Rope.verifyLines automerge
2023-11-13 06:05:36 +00:00
in
()
end
val _ = main ()