Hi,
i have a question regarding optimization:
Those code segments produce the same results, but different code sizes. Which of them is the fastest?
short X;
X&0x000f or X%16
X/16 or X>>4
X&0xfff0 or (X/16)*16
Thanx!
Printable View
Hi,
i have a question regarding optimization:
Those code segments produce the same results, but different code sizes. Which of them is the fastest?
short X;
X&0x000f or X%16
X/16 or X>>4
X&0xfff0 or (X/16)*16
Thanx!
>> X&0x000f or X%16
The first one is faster
>> X/16 or X>>4
X >> 4 will be faster
>> X&0xfff0 or (X/16)*16
The first one is faster
Premature optimisation is the root of all evil.
Your compiler will almost certainly optimise x/16 into x>>4.
Ok,
The faster statements produces the smallest code too :)
Thanx.
Most compilers will optimise multiply and divide by powers of two to the appropriate shift operations.
Go for what makes most sense when reading the code.