Hi,
I've been messing around with for loops in my code. Basically, I often have begin and end coordinates from multiple arrays that I need to move around. Example:
let a : Vec<u8> = ....
let b : Vec<u8> = ....
let c : Vec<u8> = ....
let d : Vec<u8> = ....
// and then I need to copy segment a[x..y] -> c[i..j]
// and so on
What I use are for loops and then copy character by character. I figured It is the fastest way:
use std::time::Instant;
fn main() {
let ext = |v: &mut Vec<u8>, ev: &Vec<u8>, b: usize| -> usize {
let mut x = b;
for i in ev.iter() {
v[x] = *i;
x += 1;
}
x
};
{
let before = Instant::now();
let mut a = vec![0u8; 2000000000];
for i in 0..a.len() {
a[i] = 9u8;
}
println!("Elapsed time a): {:.2?}", before.elapsed());
}
{
let before = Instant::now();
let a = vec![9u8; 10];
let mut b = Vec::new();
for i in 0..200000000 {
b.extend(a.clone());
}
println!("Elapsed time b): {:.2?}", before.elapsed());
}
{
let before = Instant::now();
let a = vec![9u8; 10];
let mut b = vec![0u8; 2000000000];
let mut y = 0;
for i in 0..200000000 {
y = ext(&mut b, &a, y);
}
println!("Elapsed time c): {:.2?}", before.elapsed());
}
{
let before = Instant::now();
let mut b = Vec::with_capacity(2000000000);
for i in 0..2000000000 {
b.push(9u8);
}
println!("Elapsed time d): {:.2?}", before.elapsed());
}
}
Runtimes:
Elapsed time a): 949.04ms
Elapsed time b): 6.08s
Elapsed time c): 1.52s
Elapsed time d): 4.81s
But having these for loops looks nasty... What I consider clean, is the extend method but it's 6x slower and has 30-40% larger memory footprint than "a)" is there some other way to extend a vector without directly copy/pasting characters as in case of for loops?
thnx in advance