Created
December 6, 2019 23:19
-
-
Save timetocode/ce4964c7201dcf385b2b10f6d2471d54 to your computer and use it in GitHub Desktop.
node & rust 500,000 entity iteration
This file contains hidden or 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
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`) |
This file contains hidden or 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::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