add basic ECS benchmark
authoralfadur
Thu, 23 Mar 2023 23:41:26 +0300
changeset 15935 cd3d16905e0e
parent 15934 d754d03e4dfe
child 15936 c5c53ebb2d91
add basic ECS benchmark
rust/hwphysics/Cargo.toml
rust/hwphysics/benches/ecs_bench.rs
rust/hwphysics/src/lib.rs
--- a/rust/hwphysics/Cargo.toml	Thu Mar 23 16:21:39 2023 -0400
+++ b/rust/hwphysics/Cargo.toml	Thu Mar 23 23:41:26 2023 +0300
@@ -8,3 +8,10 @@
 fpnum = { path = "../fpnum" }
 integral-geometry = { path = "../integral-geometry" }
 land2d = { path = "../land2d" }
+
+[dev-dependencies]
+criterion = "0.4.0"
+
+[[bench]]
+name = "ecs_bench"
+harness = false
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/hwphysics/benches/ecs_bench.rs	Thu Mar 23 23:41:26 2023 +0300
@@ -0,0 +1,36 @@
+use criterion::{black_box, criterion_group, criterion_main, Criterion};
+use hwphysics::{
+    common::GearId,
+    data::{GearDataManager}
+};
+
+#[derive(Clone, Copy, Default)]
+struct V {
+    position: u64,
+    velocity: u64
+}
+
+pub fn benchmark(c: &mut Criterion) {
+    const SIZE: usize = 4 * 1024;
+    let mut items = [V::default(); SIZE];
+
+    c.bench_function("array run", |b| b.iter(|| {
+        for item in &mut items {
+            item.velocity += black_box(item.position);
+        }
+    }));
+
+    let mut manager = GearDataManager::new();
+    manager.register::<V>();
+    for i in 1..=items.len() {
+        let gear_id = GearId::new(i as u16).unwrap();
+        manager.add(gear_id, &items[i - 1]);
+    }
+
+    c.bench_function("component run", |b| b.iter(|| {
+        manager.iter().run(|(item,): (&mut V,)| item.velocity += black_box(item.position) );
+    }));
+}
+
+criterion_group!(benches, benchmark);
+criterion_main!(benches);
\ No newline at end of file
--- a/rust/hwphysics/src/lib.rs	Thu Mar 23 16:21:39 2023 -0400
+++ b/rust/hwphysics/src/lib.rs	Thu Mar 23 23:41:26 2023 +0300
@@ -1,6 +1,6 @@
 pub mod collision;
 pub mod common;
-mod data;
+pub mod data;
 mod grid;
 pub mod physics;