## Why does this improve performance?

Apprentice
Posts: 12
Joined: 2008.11
Post: #1
Hi all,

Quick question hopefully - not a problem, just a quest for some understanding...

I had some code to get the distance between two objects which resulted in some horrendous performance (running around 500 times per cycle at 30 fps). Originally, I had something like the following:

CGFLoat distance = sqrt(((position2.x-position1.x)*(position2.x-position1.x))+((position2.y-position1.y)*(position2.y-position1.y)));

I originally thought it may be something to do with the sqrt() function call, so changed it to work with distance^2 instead. No real noticeable performance gain.

Splitting the expression out into smaller chunks, however, made a huge difference! For example:

CGFloat distX = (position2.x-position1.x);
CGFloat distY = (position2.y-position1.y);
CGFloat distXSq = distX * distX;
CGFloat distYSq = distY * distY;
CGFLoat distanceSq = distXSq+distYSq;

Why does this make so much difference to the performance?? Anyone have any ideas?

Thanks,

Dunc.
Sage
Posts: 1,199
Joined: 2004.10
Post: #2
Off the top of my head, you're only computing dx and dy once instead of twice.
Moderator
Posts: 1,559
Joined: 2003.10
Post: #3
If you're really curious, you could compile both to assembly and compare the outputs.

Do you have the appropriate compiler optimization flags enabled (-O2/-O3/-Os, -ffast-math, etc.)?
Apprentice
Posts: 12
Joined: 2008.11
Post: #4
Blimey - I have no idea what all that means! Maybe I should just accept that it works, and is much more readable!

Cheers,

D.
Member
Posts: 26
Joined: 2008.04
Post: #5
Because in the second example you're not calling sqrt()? I always learned that that was a costly function that should be avoided. *shrugs*

Mark
Member
Posts: 509
Joined: 2002.05
Post: #6
Would the -O2 O3 or Os flags do that type of optimization automatically?

I'm guessing you wouldn't want to optimize anything yourself in -O0, I always use Shark with optimization flags to find the slow points in my code.
Member
Posts: 87
Joined: 2006.08
Post: #7
If you're doing lots of FP work, make sure you build the app using the ARM instruction set, rather than Thumb. Thumb has no FP support, and so extra work must be done.
Sage
Posts: 1,231
Joined: 2002.10
Post: #8
duncster Wrote:Quick question hopefully - not a problem, just a quest for some understanding...

If you really want to understand this, you should look at the assembly generated by the compiler.

It will be a fun quest.
Member
Posts: 749
Joined: 2003.01
Post: #9
kiwiMark Wrote:Because in the second example you're not calling sqrt()?

That seems a good bet. Typically you want to use dist_square if possible (for instance (dist<r) is equivalent to (dist_square<r*r)

Â©hâ‚¬ck Ã¸ut Âµy stuÆ’Æ’ Ã¥t ragdollsoft.com