/* Copyright 2018 Johannes Boczek Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #![feature(test)] extern crate aecs; extern crate test; use aecs::*; use test::Bencher; struct Position { x: f32, y: f32 } struct Velocity { x: f32, y: f32 } struct PhysicsSystem; struct RenderSystem; impl Component for Position { const ID: u8 = 0; } impl Component for Velocity { const ID: u8 = 1; } impl System<()> for PhysicsSystem { fn handle(&mut self, _: &mut (), entity: &mut Entity) -> bool { let mut pos = entity.get_mut::() .unwrap(); let vel = entity.get::() .unwrap(); pos.x += vel.x; pos.y += vel.y; false } fn bitmask(&self) -> u64 { Position::BITMASK | Velocity::BITMASK } } impl System<()> for RenderSystem { fn handle(&mut self, _: &mut (), _: &mut Entity) -> bool { false } fn bitmask(&self) -> u64 { Position::BITMASK } } fn build_world() -> World<()> { let mut world = World::new(); world.register(PhysicsSystem); world.register(RenderSystem); for i in 0 .. 10000 { let mut entity = Entity::new(); entity.add(Position { x: 0.0, y: 0.0 }); if i % 10 == 0 { entity.add(Velocity { x: 0.0, y: 0.0 }); } world.add(entity); } world } #[bench] fn bench_build(bencher: &mut Bencher) { bencher.iter(build_world); } #[bench] fn bench_ecs(bencher: &mut Bencher) { let mut world = build_world(); bencher.iter(|| world.dispatch(&mut ())); }