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!
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.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.