I've been extremely selective about what I post here versus what I can ask on Mibbit. I hope this is within the brackets of an acceptable post. I'm creating this post to get more perspective on what i'm doing and also because I've seen many solutions but no reasoning to why referencing performance.
I started creating 2D vectors using an example I found here. I wanted my method to be a bit more flexible allowing adjustable dimensions, iterations, initializations and such. I came up with many versions (thanks to the guys/ladies on Mibbit) and eventually came to the conclusion that creating a 1D vector and iterating over it in a 2D behavior was the best way.
Is there anything I'm doing wrong here or possibly could do better in improving the performance? I shouldn't need to import a crate to do something so simple. So if there's a way I can improve my method would you be so kind to elaborate.
Profiling: 539,967 ns/iter (+/- 40,988)
const IMG_WIDTH: usize = 2052;
const IMG_HEIGHT: usize = 2048;
pub fn create_2d_vector (pixel_2d_vec: &mut Vec<Vec<u16>>, width: &usize, height: &usize) { println!("\n\nCreating nested 2D vector of {}_WIDTH by {}_HEIGHT ", width, height); for (_i, row) in pixel_2d_vec.iter_mut().enumerate() { for (_y, col) in row.iter_mut().enumerate() { *col += _y as u16; } } }
[EDIT]
My apologies, the above was one version I had but I eventually got to the following version which is continuous layout "I believe".
Profiling: 439,495 ns/iter (+/- 35,042)
const IMG_WIDTH: usize = 2052;
const IMG_HEIGHT: usize = 2048;
pub fn create_1d_vector (pixel_1d_vec: &mut Vec<u16>, width: &usize, height: &usize) {
println!("\n\nCreating 1D Vec<u16> of {}_WIDTH X {}_HEIGHT ", width, height);
for x in pixel_1d_vec.chunks_mut(*width) {
for (iter, item) in x.iter_mut().enumerate() {
*item += iter as u16;
}
}
}