Skip to content

Commit a4ed510

Browse files
authored
Flyweight: print physical memory measurement (fadeevab#7)
1 parent ab4843c commit a4ed510

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

structural/flyweight/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ path = "main.rs"
99

1010
[dependencies]
1111
draw = "0.3"
12+
memory-stats = "1.0.0"
1213
rand = "0.8"

structural/flyweight/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ Memory usage:
3232
Tree size (16 bytes) * 100000
3333
+ TreeKind size (~30 bytes) * 2
3434
-------------------------------
35-
Total: 1MB (instead of 4MB)
35+
Total: 1688KB (estimated 1562KB),
36+
instead of 4492KB
3637
```
3738

3839
## Overview

structural/flyweight/main.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const TREE_TYPES: u32 = 2;
1212
fn main() {
1313
let forest = &mut Forest::default();
1414

15+
let phys_mem_before = memory_stats::memory_stats().unwrap().physical_mem;
16+
1517
for _ in 0..TREES_TO_DRAW / TREE_TYPES {
1618
let mut rng = rand::thread_rng();
1719

@@ -32,6 +34,8 @@ fn main() {
3234
);
3335
}
3436

37+
let phys_mem_after = memory_stats::memory_stats().unwrap().physical_mem;
38+
3539
let mut canvas = Canvas::new(CANVAS_SIZE, CANVAS_SIZE);
3640
forest.draw(&mut canvas);
3741

@@ -45,8 +49,9 @@ fn main() {
4549
println!("+ TreeKind size (~30 bytes) * {}", TREE_TYPES);
4650
println!("-------------------------------");
4751
println!(
48-
"Total: {}MB (instead of {}MB)",
49-
((TREES_TO_DRAW * 16 + TREE_TYPES * 30) / 1024 / 1024),
50-
((TREES_TO_DRAW * 46) / 1024 / 1024)
52+
"Total: {}KB (estimated {}KB),\n instead of {}KB",
53+
(phys_mem_after - phys_mem_before) / 1024,
54+
(TREES_TO_DRAW * 16 + TREE_TYPES * 30) / 1024,
55+
((TREES_TO_DRAW * 46) / 1024)
5156
);
5257
}

0 commit comments

Comments
 (0)