Files
dllist/test/benchmark.gleam
Gareth Pendleton 0fde06efe7
Some checks failed
test / test (push) Has been cancelled
feat: More rework
So the performance of this only surpasses list when you approach 100
items (and that is not including the need to traverse through the linked
list to find the insertion point) so this might be the last commit
2025-10-20 19:33:38 +01:00

114 lines
2.8 KiB
Gleam

import dllist
import gleam/list
import glychee/benchmark
import glychee/configuration
@target(erlang)
pub fn main() {
configuration.initialize()
configuration.set_pair(configuration.Warmup, 2)
configuration.set_pair(configuration.Parallel, 2)
benchmark2()
}
@target(erlang)
fn benchmark() {
benchmark.run(
[
benchmark.Function(
label: "list append",
callable: fn(data: #(List(Int), Int)) {
fn() {
list.fold(data.0, [], fn(acc, i) { [i, ..acc] })
|> list.reverse
|> insert_at(data.1, 999)
Nil
}
},
),
benchmark.Function(
label: "dllist insert",
callable: fn(data: #(List(Int), Int)) {
fn() {
list.fold(data.0, dllist.new(), fn(acc, i) { dllist.insert(acc, i) })
|> move_left_n(data.1)
|> dllist.insert(999)
Nil
}
},
),
],
[
benchmark.Data(label: "6 Items", data: #(list.range(1, 6), 3)),
benchmark.Data(label: "10 Items", data: #(list.range(1, 10), 5)),
benchmark.Data(label: "100 Items", data: #(list.range(1, 100), 50)),
],
)
}
@target(erlang)
fn benchmark2() {
benchmark.run(
[
benchmark.Function(
label: "list append",
callable: fn(data: #(List(Int), dllist.DLList(Int), Int)) {
fn() {
let _ = insert_at(data.0, data.2, 999)
Nil
}
},
),
benchmark.Function(
label: "dllist insert",
callable: fn(data: #(List(Int), dllist.DLList(Int), Int)) {
fn() {
let _ = dllist.insert(data.1, 999)
Nil
}
},
),
],
[
benchmark.Data(label: "6 Items", data: #(
list.range(1, 6),
dllist.from_list(list.range(1, 6)) |> move_left_n(3),
3,
)),
benchmark.Data(label: "10 Items", data: #(
list.range(1, 10),
dllist.from_list(list.range(1, 10)) |> move_left_n(5),
5,
)),
benchmark.Data(label: "100 Items", data: #(
list.range(1, 100),
dllist.from_list(list.range(1, 100)) |> move_left_n(50),
50,
)),
benchmark.Data(label: "1000 Items", data: #(
list.range(1, 1000),
dllist.from_list(list.range(1, 1000)) |> move_left_n(500),
500,
)),
benchmark.Data(label: "10000 Items", data: #(
list.range(1, 10_000),
dllist.from_list(list.range(1, 10_000)) |> move_left_n(5000),
5000,
)),
],
)
}
fn insert_at(list: List(a), pos: Int, value: a) -> List(a) {
let #(l1, l2) = list.split(list, pos)
list.append(l1, [value, ..l2])
}
fn move_left_n(list: dllist.DLList(a), n: Int) -> dllist.DLList(a) {
case n {
0 -> list
n -> move_left_n(dllist.move_left(list), n - 1)
}
}