Skip to main content
Code Review

Return to Revisions

3 of 4
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/

If we look at your for loop very carefully we can see that you can eliminate two multiplications:

for (int y = 0; y < height; y++) {
 for (int x = 0; x < width; x++) {
 for (int color = 0; color < 3; color++) {
 int idx = (y * stride) + x * 4 + color;
 totals[color] += p[idx];
 }
 }
}

You don't use y except for y * stride, and you don't use x except for x * 4, rewrite the for loop and you can eliminate those entirely.

var heightLimit = height * stride;
var widthLimit = width * 4;
for (int y = 0; y < heightLimit; y += stride) {
 for (int x = 0; x < widthLimit; x += 4) {
 for (int color = 0; color < 3; color++) {
 int idx = y + x + color;
 totals[color] += p[idx];
 }
 }
}

By removing all three multiplication operations, we effectively reduce the amount of work we do by a significant portion. (There are roughly 8 instructions in the original, and 6 in the reduced, so we've eliminated 25% of our work.)

Other than that, there really isn't much you can do. You could consider chunking the work, then only recalculate the regions that were re-drawn if that's reasonable, you could also chunk and thread it (this wouldn't reduce CPU usage but it would reduce time spend on this method).

With as frequently as you call this method, the next lines may be a potential for optimization as well:

int avgB = totals[0] / (width * height);
int avgG = totals[1] / (width * height);
int avgR = totals[2] / (width * height);

Why do width * height in all three calculations? Why not store var pixelCount = width * height; then divide by pixelCount? Of course, division is still slow, but you're not using floating-point math so we can't use the reciprocal of it.

You could consider, as mentioned in a comment, using CUDA/OpenGL/GPU-level work. Basically, operating on the GPU itself instead of using the CPU to do what could be very efficient on the GPU. (It's built specifically for this type of processing.) There is at least one Stack Overflow question on running C# code on the GPU, it's not very easy or simple, but it can give you a lot of power.

Der Kommissar
  • 20.3k
  • 4
  • 70
  • 158
default

AltStyle によって変換されたページ (->オリジナル) /