Optimizing code

Started by Shadow, June 03, 2009, 10:20:18 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Shadow

I have a bit of code I am working on and speed is an issue. Right now I have a sqrt inside a double loop, which makes scaling kinda crappy. I can't get around the sqrt, but I wonder if there are ways to save on divisions.

Question:

if I am doing the computation x = y / foo(bar) where foo is some function, would it be cheaper to have foo return 1 / (usual return value) and make that line:

x = y * foo(bar)?

Also, is 1 / x significantly cheaper/more expensive than y / x     y != 1?
<=holbs-.. ..-holbs=> <=holbs-..

bjornredtail

Nope. They would both translate to a very similar series of CPU instructions.

Is there any way out of the double loop?

Perhaps you could do this the lazy way and let the compiler try to optimize it. Look up what the -o1 through -o4 flags do in GCC. I also remember a way of profiling a piece of code using GDB and running the results into the compiler to help it decide things like which is the more common result from an if statement. I can't remember /how/ to do it though.
0==={=B=J=O=R=N=R=E=D=T=A=I=L==>
AKA, Nevadacow
First person to ever play RWL

"Program testing can be used to show the presence of bugs, but never to show their absence!"-Edsger W. Dijkstra

Visit http://frostnflame.org today!