by vorachose · 7y ago · 0 comments
for x in 0..w {
for y in 0..h {
let mut rgba = [0; 4];
[...]
to let mut rgba = [0; 4];
for y in 0..h {
for x in 0..w {
[...]
cuts down runtime by between 35 and 50% on my side.EDIT : moving the call to get_pixel outside the inner loop take off another ~10%, bringing it to sub 0.145 from ~0.290.
let mut rgba = [0; 4];
for y in 0..h {
for x in 0..w {
let pix1 = image1.get_pixel(x, y);
let pix2 = image2.get_pixel(x, y);
for c in 0..4 {
rgba[c] = abs_diff(
pix1.data[c],
pix2.data[c],
);
}
let new_pix = image::Pixel::from_slice(&rgba);
diff.put_pixel(x, y, *new_pix);
}
}At least that is my understanding of how to implement a cache oblivious algorithm. See https://en.wikipedia.org/wiki/Cache-oblivious_algorithm
Used that approach once when computing a large cross product and it gave a good speed up.