Skip to content

Commit 7c497fd

Browse files
committed
feat(lib): adding std.Benchmark, with macros to run benchmarks
1 parent d4286b3 commit 7c497fd

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Benchmark.ark

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# @brief Measure the time it takes to run some given code, in milliseconds
2+
# @param tag Identifier for the code block (string)
3+
# @param code Node of code to run
4+
# @author https://github.com/SuperFola
5+
(macro measureOnce (tag code) {
6+
(mut _start (time))
7+
{ code }
8+
(print (format "{} took {:.4} ms" tag (* 1000 (- (time) _start)))) })
9+
10+
(import std.List)
11+
12+
# @brief Benchmark some given code by running it a given number of times
13+
# @param code code to run, eg. a function call
14+
# @param times number of times to run the code
15+
# =begin
16+
# (let fib (fun (n)
17+
# (if (< n 2)
18+
# n
19+
# (+ (fib (- n 1)) (fib (- n 2))))))
20+
# (bench (fib 23) 10)
21+
# # (fib 23), 10 times
22+
# # ↪︎ range: [4.7 - 5.02] ms
23+
# # ↪︎ mean: 4.85ms
24+
# # ↪︎ median: 4.86ms
25+
# =end
26+
# @author https://github.com/SuperFola
27+
(macro bench (code times) {
28+
(mut _i 0)
29+
(mut _data [])
30+
(while (< _i times) {
31+
(let _start (time))
32+
{ ($as-is code) }
33+
(let _end (time))
34+
(append! _data (* 1000 (- _end _start)))
35+
(set _i (+ 1 _i)) })
36+
37+
(mut _mean (/ (list:sum _data) (len _data)))
38+
(mut _median (list:median _data))
39+
(mut _max (list:max _data))
40+
(mut _min (list:min _data))
41+
(print
42+
(format
43+
"{}, {} times\n ↪︎ range: [{:.3} - {:.3}] ms\n ↪︎ mean: {:.3}ms\n ↪︎ median: {:.3}ms"
44+
($repr code) times _min _max _mean _median)) })

0 commit comments

Comments
 (0)