fn calc_cells_recursively(last_cells:Vec<Vec<u8>>, dim:usize, dimensions:usize, end:&Vec<u8>)->Vec<Vec<u8>>{
if dim == dimensions {
return last_cells;
}
let mut next_gen_cells:Vec<Vec<u8>> = Vec::new();
for cell in last_cells.iter() {
for v in cell[dim]..=end[dim]{
let mut new_cell=cell.clone();
new_cell[dim] = v;
next_gen_cells.push(new_cell);
}
}
return calc_cells_recursively(next_gen_cells, dim + 1, dimensions, end);
}
fn calc_cells(start:Vec<u8>, end:Vec<u8>)->Vec<Vec<u8>>{
let mut last_cells:Vec<Vec<u8>>=Vec::new();
let len=start.len();
last_cells.push(start);
return calc_cells_recursively(last_cells, 0usize, len, &end);
}
fn main(){
let start=vec![0, 0, 0,0 ];
let end=vec![127, 127, 127, 29];
let all_cells=calc_cells(start, end);
}
The codes above are just to calculate all points in N dimensional space ranging from 'start' to 'end' in a recursive way. It takes 92083 milliseconds, but Java only takes about 11055 milliseconds, and Python takes 124420 milliseconds, by following the same logics with the same input in the same environment. The perf gap is not trivial at all between Rust and Java, what's more, Rust doesn't beat Python too much. I guess it would be faster if pure arrays could be used, but Rust needs to determine a static array size in compiling time, so could anyone help me to optimize these codes to speed it up significantly?, please. I really hope Rust can beat Java on this specific problem, just because I'm not experienced enough in Rust.