Skip to content

Instantly share code, notes, and snippets.

@timetocode
Created December 6, 2019 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timetocode/ce4964c7201dcf385b2b10f6d2471d54 to your computer and use it in GitHub Desktop.
Save timetocode/ce4964c7201dcf385b2b10f6d2471d54 to your computer and use it in GitHub Desktop.
node & rust 500,000 entity iteration
const createEntity = (nid, x, y, name) => {
return {
nid,
x,
y,
hp: 100,
name
}
}
const entities = []
for (let i = 0; i < 500000; i++) {
entities.push(createEntity(i, 50, 50, `entity${i}`))
}
const start = Date.now() // not a high resolution timer
entities.forEach(entity => {
entity.x += 5
entity.y += 5
})
console.log(`took ${Date.now() - start}ms`)
use std::time::Instant;
#[derive(Debug)]
struct Entity {
nid: u32,
x: f64,
y: f64,
hp: u8,
name: String
}
fn create_entity(nid: u32, x: f64, y: f64, name: String) -> Entity {
Entity {
nid,
x,
y,
hp: 100,
name
}
}
fn main() {
let mut entities: Vec<Entity> = Vec::new();
for nid in 1..500000 {
entities.push(create_entity(nid, 50.0, 50.0, String::from("entity") + &nid.to_string()));
}
let start = Instant::now();
for entity in entities.iter_mut() {
entity.x += 5.0;
entity.y += 5.0;
}
println!("took {:?}", Instant::now() - start);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment