-
Notifications
You must be signed in to change notification settings - Fork 0
/
glychee_example_benchmark_module.gleam
52 lines (48 loc) · 1.51 KB
/
glychee_example_benchmark_module.gleam
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
52
//// Example module not to be called directly but could be mimicked in your
//// application or library.
////
if erlang {
import glychee/benchmark
import gleam/list
import gleam/int
/// Main function so that this call can succeed:
/// `erl -pa ./build/dev/erlang/*/ebin -noshell -eval 'gleam@@main:run(glychee_example_benchmark_module)'`
///
pub fn main() {
benchmark.run(
[
benchmark.Function(
label: "list.sort()",
fun: fn(test_data) { fn() { list.sort(test_data, int.compare) } },
),
benchmark.Function(
label: "erlang:lists:sort()",
fun: fn(test_data) { fn() { erlang_lists_of_int_sort(test_data) } },
),
],
[
benchmark.Data(label: "pre-sorted list", data: list.range(1, 100_000)),
benchmark.Data(
label: "reversed list",
data: list.range(1, 100_000)
|> list.reverse,
),
benchmark.Data(
label: "shuffled list",
data: list.range(1, 100_000)
|> elixir_enum_shuffle,
),
],
)
}
/// Helper function to shuffle a list,
/// because at the time of writing gleam_stdlib did not feature list.shuffle.
///
external fn elixir_enum_shuffle(List(a)) -> List(a) =
"Elixir.Enum" "shuffle"
/// A wrapper for Erlang's built-in list sorting implementation.
/// Used as a bechmark baseline/comparsion to gleam's stdlib list.sort().
///
external fn erlang_lists_of_int_sort(List(a)) -> List(a) =
"lists" "sort"
}