Skip to content

Commit 0620f32

Browse files
committed
chore: add example with allocations
1 parent f3d7310 commit 0620f32

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

crates/divan_compat/examples/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ harness = false
4444
[[bench]]
4545
name = "counters"
4646
harness = false
47+
48+
[[bench]]
49+
name = "alloc"
50+
harness = false
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use std::{
2+
alloc::Layout,
3+
collections::{HashMap, HashSet},
4+
};
5+
6+
#[divan::bench]
7+
fn allocate() {
8+
println!("Hello, world!");
9+
10+
let vec = vec![1, 2, 3];
11+
println!("{vec:?}");
12+
13+
let mut map = HashMap::new();
14+
map.insert("key", "value");
15+
println!("{map:?}");
16+
17+
let mut set = HashSet::new();
18+
set.insert("apple");
19+
set.insert("banana");
20+
println!("{set:?}");
21+
22+
std::thread::sleep(std::time::Duration::from_secs(1));
23+
24+
let mut bytes_vec = vec![0u8; 0x100];
25+
println!("{:?}", bytes_vec.len());
26+
27+
bytes_vec.extend(&vec![0u8; 0x1000]);
28+
29+
// Alloc 256 bytes of memory
30+
for _ in 0..100 {
31+
let memory = unsafe { std::alloc::alloc(Layout::new::<[u8; 42]>()) };
32+
core::hint::black_box(memory);
33+
}
34+
}
35+
36+
fn main() {
37+
divan::main();
38+
}

0 commit comments

Comments
 (0)