I'm making a lookup table (1 x 256 array of unsigned chars) to record all unique pixel values found in an image. I initialize the array to all 0s; then I want to write 1 to each element where the array index equals a pixel value in the image.
I can simply scan the image and write 1 to the array every time, resulting in many unnecessary writes since if, for example, the value 155 appears 15,000 times in the image I'm writing 1 to table[155] 15,000 times.
Or, I can put the assignment in an if statement and write a 1 only if that table entry is 0, so when 155 is read from the image, I test table[155], write 1 to table[155] only 1 time, but subsequently test it in the if statement 14,999 additional times.
Which takes less time?