I'm not really a programmer and very new to Rust so I assume I'm doing something wrong here. While sorting an array of 1M bytes my Rust function takes 3.5s and the C function only takes 0.2s. This is too big difference for me to believe that Rust is just that much slower. Need sanity check of my code please, what am I missing? I tried replacing the Rust swap
function call with directly swapping values like I have done in the C implementation but it makes run even slower.
Rust implementation:
fn sort_bubble(sample: &mut Vec<u8>) -> &mut Vec<u8> {
let mut updated = true;
let sample_len = sample.len();
let mut i;
while updated {
i = 0;
updated = false;
while i < sample_len - 1 {
if sample[i + 1] < sample[i] {
sample.swap(i, i + 1);
updated = true;
}
i += 1;
}
}
sample
}
C implementation:
u_int8_t* sort_bubble(u_int8_t* sample, int sample_len)
{
char updated = 1;
while(updated) {
int i = 0;
updated = 0;
while(i < sample_len - 1) {
if(sample[i + 1] < sample[i]) {
u_int8_t _ = sample[i];
sample[i] = sample[i + 1];
sample[i + 1] = _;
updated = 1;
}
i++;
}
}
return sample;
}