#Optimal iterator use with converting indexes
14 messages · Page 1 of 1 (latest)
pub fn generate_noisemap2D (&self, x_start: f32, y_start: f32, map: &mut [f32], map_width: usize) {
for (i, elem) in map.iter_mut().enumerate() {
*elem = self.generate2D(x_start + (i % map_width) as f32, y_start + (i / map_width) as f32);
}
}
What I'm trying to do is iterate through a collection, generate a noise value with generate2D, and assign it to it's corresponding element
map, our target collection, is 1-dimensional
but generate2D takes an x and y
which means we have to convert the indeces
however, converting the indices does take time
for x in 0..map_width {
for y in 0..(map.len() / map_width) {
map[x + map_width * y] = self.generate2D(x_start + x as f32, y_start + y as f32);
}
}
I can also do it this way
but I'm not sure which way is better, or faster
actually
it's pretty obvious the second one is better
Sorry if I wasted anyone's time