-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBenchmark.ark
More file actions
51 lines (48 loc) · 1.35 KB
/
Benchmark.ark
File metadata and controls
51 lines (48 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# @brief Measure the time it takes to run some given code, in milliseconds
# @param tag Identifier for the code block (string)
# @param code Node of code to run
# @author https://github.com/SuperFola
(macro measureOnce (tag code) {
(mut _start (time))
{
code }
(print (format "{} took {:.4} ms" tag (* 1000 (- (time) _start)))) })
(import std.List)
# @brief Benchmark some given code by running it a given number of times
# @param code code to run, eg. a function call
# @param times number of times to run the code
# =begin
# (let fib (fun (n)
# (if (< n 2)
# n
# (+ (fib (- n 1)) (fib (- n 2))))))
# (bench (fib 23) 10)
# # (fib 23), 10 times
# # ↪︎ range: [4.7 - 5.02] ms
# # ↪︎ mean: 4.85ms
# # ↪︎ median: 4.86ms
# =end
# @author https://github.com/SuperFola
(macro bench (code times) {
(mut _i 0)
(mut _data [])
(while (< _i times) {
(let _start (time))
{
($as-is code) }
(let _end (time))
(append! _data (* 1000 (- _end _start)))
(set _i (+ 1 _i)) })
(mut _mean (/ (list:sum _data) (len _data)))
(mut _median (list:median _data))
(mut _max (list:max _data))
(mut _min (list:min _data))
(print
(format
"{}, {} times\n ↪︎ range: [{:.3} - {:.3}] ms\n ↪︎ mean: {:.3}ms\n ↪︎ median: {:.3}ms"
($repr code)
times
_min
_max
_mean
_median)) })