Created
November 30, 2023 08:11
-
-
Save stepancheg/06d4e9ea9d48d17063d79b10be4405dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::hint; | |
use bevy::prelude::*; | |
#[derive(Component, Copy, Clone)] | |
struct Transform(Mat4); | |
#[derive(Component, Copy, Clone)] | |
struct Position(Vec3); | |
#[derive(Component, Copy, Clone)] | |
struct Rotation(Vec3); | |
#[derive(Component, Copy, Clone)] | |
struct Velocity(Vec3); | |
pub struct Benchmark<'w>(World, QueryState<(&'w Velocity, &'w mut Position)>); | |
impl<'w> Benchmark<'w> { | |
pub fn new() -> Self { | |
let mut world = World::new(); | |
// TODO: batch this | |
for _ in 0..10_000 { | |
world.spawn(( | |
Transform(Mat4::from_scale(Vec3::ONE)), | |
Position(Vec3::X), | |
Rotation(Vec3::X), | |
Velocity(Vec3::X), | |
)); | |
} | |
let query = world.query::<(&Velocity, &mut Position)>(); | |
Self(world, query) | |
} | |
#[inline(never)] | |
pub fn run(&mut self) { | |
for (velocity, mut position) in self.1.iter_mut(&mut self.0) { | |
position.0 += velocity.0; | |
} | |
} | |
} | |
fn main() { | |
for _ in 0..5000 { | |
let mut benchmark = Benchmark::new(); | |
hint::black_box(&mut benchmark); | |
benchmark.run(); | |
hint::black_box(benchmark); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment