Skip to content

Commit 40dd06b

Browse files
committed
docs: add vector benchmark and diagram
1 parent ed52504 commit 40dd06b

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ path = "examples/soluciones/vector_insert_sorted.rs"
2424
[[example]]
2525
name = "solution_vector_retain_prefix"
2626
path = "examples/soluciones/vector_retain_prefix.rs"
27+
28+
[[bench]]
29+
name = "vector_bench"
30+
path = "benches/vector_bench.rs"
31+
harness = false

benches/vector_bench.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use rust_data_structures::vector::Vector;
2+
use std::hint::black_box;
3+
use std::time::{Duration, Instant};
4+
5+
const SIZE: usize = 20_000;
6+
7+
fn main() {
8+
println!("vector benchmark (manual, std::time::Instant)");
9+
println!("size: {SIZE}");
10+
println!("push growth: {:?}", bench_push_growth());
11+
println!("random access: {:?}", bench_random_access());
12+
println!("insert front: {:?}", bench_insert_front());
13+
println!("insert back: {:?}", bench_insert_back());
14+
}
15+
16+
fn bench_push_growth() -> Duration {
17+
let start = Instant::now();
18+
let mut values = Vector::new();
19+
20+
for value in 0..SIZE {
21+
values.push(black_box(value));
22+
}
23+
24+
black_box(values.len());
25+
start.elapsed()
26+
}
27+
28+
fn bench_random_access() -> Duration {
29+
let mut values = Vector::with_capacity(SIZE);
30+
for value in 0..SIZE {
31+
values.push(value);
32+
}
33+
34+
let start = Instant::now();
35+
let mut sum = 0usize;
36+
37+
for index in 0..SIZE {
38+
sum += *values.get(black_box(index)).expect("index inside vector");
39+
}
40+
41+
black_box(sum);
42+
start.elapsed()
43+
}
44+
45+
fn bench_insert_front() -> Duration {
46+
let start = Instant::now();
47+
let mut values = Vector::new();
48+
49+
for value in 0..SIZE {
50+
values.insert(0, black_box(value)).expect("front is valid");
51+
}
52+
53+
black_box(values.len());
54+
start.elapsed()
55+
}
56+
57+
fn bench_insert_back() -> Duration {
58+
let start = Instant::now();
59+
let mut values = Vector::new();
60+
61+
for value in 0..SIZE {
62+
values
63+
.insert(values.len(), black_box(value))
64+
.expect("len is valid insertion point");
65+
}
66+
67+
black_box(values.len());
68+
start.elapsed()
69+
}

diagrams/01-vector.mmd

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
flowchart LR
2+
title["Vector: longitud, capacidad y memoria contigua"]
3+
4+
subgraph metadata["Metadatos"]
5+
len["len = 3"]
6+
cap["capacity = 6"]
7+
end
8+
9+
subgraph memory["Bloque contiguo"]
10+
slot0["0: A"]
11+
slot1["1: B"]
12+
slot2["2: C"]
13+
slot3["3: libre"]
14+
slot4["4: libre"]
15+
slot5["5: libre"]
16+
end
17+
18+
len --> slot2
19+
cap --> slot5
20+
slot0 --> slot1 --> slot2 --> slot3 --> slot4 --> slot5
21+
22+
push["push(D)"]
23+
push --> slot3
24+
25+
grow["Si len == capacity: crecer y mover"]
26+
cap --> grow

0 commit comments

Comments
 (0)