Here's an optimization suggestion that applies in any language and does not harm readability.
Instead of this:
b = (n & 0xff0000) >> 16
g = (n & 0xff00) >> 8
r = (n & 0xff)
use this:
b = (n >> 16) & 0xff
g = (n >> 8) & 0xff
r = n & 0xff
Two reasons:
Fewer constants are not slower and can be faster.
Having smaller constants is not slower and can be faster - in a language such as C, a shorter machine instruction may be available; in a language such as Python, the implementation probably combines small integers.
source
share