Skip to content

Commit 8638bef

Browse files
committed
chore: add example with allocations
1 parent 3470ff8 commit 8638bef

File tree

2 files changed

+39
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)