c - multiply by 0.5 rather than divide by 2 -


while reading tips in c . have seen tip here http://www.cprogramming.com/tips/tip/multiply-rather-than-divide iam not sure. told both multiply , divide slower , time consuming , requires many cycles.

and have seen people use i << 2 instead of i x 4 since shifting faster.

so me this.is tip using x0.5 or /2 ? or modern compilers optimize in better way? want know views of guys stackoverflow.

also post links useful tips n traps in c. appreciated.

it's true (if not most) processors can multiply faster performing division. but, it's myth of ++i being faster i++ in loop. yes, once was, nowadays, compilers smart enough optimize things you, should not care anymore.

and bit-shifting, once faster shift << 2 multiply 4, these days over, processors can multiply in 1 clock cycle, shift operation. great example of calculation of pixel address in vga 320x240 mode. did this:

address = x + (y << 8) + (y << 6) 

to multiply y 320. on modern processors, many times slower then

address = x + y * 320; 

so, write think , compiler rest :)


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -