Skip to content

Instantly share code, notes, and snippets.

@AmineDiro
Last active October 9, 2023 20:48
Show Gist options
  • Select an option

  • Save AmineDiro/28b3fa2573ecf390dcb77b8e42d7e11e to your computer and use it in GitHub Desktop.

Select an option

Save AmineDiro/28b3fa2573ecf390dcb77b8e42d7e11e to your computer and use it in GitHub Desktop.
Struct of arrays
use rand::Rng;
use std::time::Instant;
const N: usize = 1_000_000;
const N_WARMUP: usize = 100;
struct Shapes {
rectangles: Vec<Rectangle>,
triangles: Vec<Triangle>,
squares: Vec<Square>,
}
impl Shapes {
fn total_area(&self) -> f32 {
self.rectangles
.iter()
.map(|r| r.area())
.chain(self.triangles.iter().map(|t| t.area()))
.chain(self.squares.iter().map(|s| s.area()))
.sum()
}
}
struct Square {
side: f32,
}
impl Square {
#[inline(always)]
fn area(&self) -> f32 {
self.side * self.side
}
}
struct Rectangle {
width: f32,
height: f32,
}
impl Rectangle {
#[inline(always)]
fn area(&self) -> f32 {
self.width * self.height
}
}
struct Triangle {
base: f32,
height: f32,
}
impl Triangle {
#[inline(always)]
fn area(&self) -> f32 {
0.5 * self.base * self.height
}
}
fn init_shapes() -> Shapes {
let mut rectangles: Vec<Rectangle> = Vec::new();
let mut triangles: Vec<Triangle> = Vec::new();
let mut squares: Vec<Square> = Vec::new();
let mut rng = rand::thread_rng();
for _ in 0..3 * N {
match rng.gen_range(0..3) {
0 => {
rectangles.push(Rectangle {
width: 4.0,
height: 4.0,
});
}
1 => triangles.push(Triangle {
base: 4.0,
height: 4.0,
}),
_ => squares.push(Square { side: 4.0 }),
}
}
Shapes {
rectangles,
triangles,
squares,
}
}
fn main() {
let shapes = init_shapes();
// Running benchmark
let start = Instant::now();
let mut total = 0.0;
for _ in 0..N_WARMUP {
total = shapes.total_area();
}
let duration = start.elapsed();
println!(
"{}. Struct-of-arrays took {:?}.",
total,
duration / N_WARMUP as u32
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment