Skip to content

Commit 8a037a5

Browse files
committed
add benchy
1 parent 2fce8f7 commit 8a037a5

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ fn generate_buffer(
3939
dim: WindowDimensions,
4040
offset: Complex,
4141
) {
42-
let max_pixel = dim.width * dim.height - 1;
43-
4442
let buf = UncheckedSyncArray::from_slice(buffer);
4543
let out_buf = &buf;
4644

@@ -49,7 +47,7 @@ fn generate_buffer(
4947
s.spawn(move |_| {
5048
let mut pixel = thread_id;
5149

52-
while pixel <= max_pixel {
50+
while pixel < out_buf.len() {
5351
let mut z = Complex::default();
5452
let c = index_to_complex(pixel, scale, dim, offset);
5553

@@ -122,6 +120,8 @@ fn main() -> Result<(), Box<dyn Error>> {
122120
let mut frame = 0;
123121
let mut scale = conf.starting_scale;
124122

123+
let start = std::time::Instant::now();
124+
125125
while window.is_open() && !window.is_key_down(Key::Escape) {
126126
generate_buffer(conf.threads, scale, &mut buffer, conf.dims, conf.offset);
127127
insert_frame_counter(frame, &mut buffer, conf.dims);
@@ -130,6 +130,10 @@ fn main() -> Result<(), Box<dyn Error>> {
130130
frame += 1;
131131

132132
window.update_with_buffer(&buffer, conf.dims.width, conf.dims.height)?;
133+
134+
if frame % 100 == 0 {
135+
println!("{frame} frames in {:?}", start.elapsed());
136+
}
133137
}
134138

135139
Ok(())

src/unchecked_array.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(dead_code)]
2+
13
pub struct UncheckedSyncArray<'a, T>(*mut T, usize, core::marker::PhantomData<&'a mut T>);
24

35
unsafe impl<'a, T: Send + Sync> Sync for UncheckedSyncArray<'a, T> {}
@@ -7,6 +9,14 @@ impl<'a, T> UncheckedSyncArray<'a, T> {
79
UncheckedSyncArray(v.as_mut_ptr(), v.len(), core::marker::PhantomData)
810
}
911

12+
pub fn len(&self) -> usize {
13+
self.1
14+
}
15+
16+
pub fn is_empty(&self) -> bool {
17+
self.len() == 0
18+
}
19+
1020
/// # Safety:
1121
/// As this has no mechanism to ensure more than 1 thread accesses the same index at a time,
1222
/// if more than 1 thread accesses the same index at a time UB will occur.

0 commit comments

Comments
 (0)