Skip to content

Commit

Permalink
Add parallel iteration example
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantMoyer committed Aug 22, 2020
1 parent 3a92f31 commit 808a242
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ path = "examples/ecs/startup_system.rs"
name = "ecs_guide"
path = "examples/ecs/ecs_guide.rs"

[[example]]
name = "parallel_query"
path = "examples/ecs/parallel_query.rs"

[[example]]
name = "breakout"
path = "examples/game/breakout.rs"
Expand Down
33 changes: 33 additions & 0 deletions examples/ecs/parallel_query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use bevy::{ecs::rayon::prelude::*, prelude::*};
use std::{
thread,
time::{Duration, Instant},
};

fn spawn_system(mut commands: Commands) {
for i in 0..16usize {
commands.spawn((i,));
}
}

fn square_system(mut nums: Query<&mut usize>) {
nums.iter().into_par_iter().for_each(|mut n| {
thread::sleep(Duration::from_secs(1));
*n = *n * *n;
});
}

fn print_system(num: &usize) {
print!("{} ", num);
}

fn main() {
let t0 = Instant::now();
App::build()
.add_startup_system(spawn_system.system())
.add_system(square_system.system())
.add_system(print_system.system())
.run();
let t1 = Instant::now();
println!("\nTook {:.3}s", (t1 - t0).as_secs_f32());
}

0 comments on commit 808a242

Please sign in to comment.