【发布时间】:2020-05-24 23:58:37
【问题描述】:
显然,std 库的实现总是比我的要快得多,但是在快速计算机上完成 773 x 1031 图像需要 3 分钟左右。使用相同的比较器,标准库需要 4 秒来对整个图像进行排序。任何想法为什么我的代码这么慢?
fn partial_quick_sort(img: &mut Bitmap, cmp: &BoxedPixelComparator) {
// buffer is a Vec of Pixel structs - Pixel { r: u8, g: u8, b: u8, a: u8 }
let max_depth = img.buffer.len()/200;
println!("{}", max_depth); // For the input image I'm testing with, this prints 3984
let mut queue = VecDeque::new();
queue.push_back(img.buffer.as_mut_slice());
let mut depth = 0;
loop {
let buffer_opt = queue.pop_front();
if buffer_opt.is_none() {
break;
}
let buffer = buffer_opt.unwrap();
if depth >= max_depth || buffer.len() <= 1 {
continue;
}
let pivot_idx = buffer.len() - 1;
// cmp is a comparator function, which just compares the sums of two pixels
let (left, _pivot, right) = buffer.partition_at_index_by(pivot_idx, cmp);
queue.push_back(left);
queue.push_back(right);
depth +=1;
}
}
【问题讨论】:
-
盲答:你忘了使用 --release ?