views:

155

answers:

4

I have this piece of code where Shark tells me it's a performance bottleneck:

CGFloat shortestDistance = (distanceA < distanceB) ? distanceA : distanceB;

all these values are CGFloat. Is there a faster way to figure out which one is smaller and assign that to shortestDistance? Maybe even by reference instead of copying a value? How would I do that and how would I access that later?

btw, this is code executed in a very tight loop. about 60 times per second.

+10  A: 

60 times per second is not a tight loop. That's pretty relaxed, as far as modern computers are concerned.

There doesn't seem to be an obvious way to improve that expression; are you sure that Shark is indicating that exact statement is a problem, or is it pointing out a more general problem with code that gets called frequently?

Greg Hewgill
A: 

From memory modern CPU's have 6 to 9 pipelines, even more probably since the last time I checked. What you would normally consider to be heavy CPU is in fact very little once the optimizer gets to it.

Chad
+7  A: 

What your likely running into is the thumb/arm FPU issue. iPhone apps default to compiling to thumb, which is a 16 bit subset of the regular ARM expressions. When in thumb mode, floating point operations are done using integer routines. You can switch this off and increase your floating point performance. See "Break That Thumb For Best iPhone Performance".

alt text

brianegge
+3  A: 

Is either distanceA or distanceB (or both) the result of a square root? A shark trace will often blame the operation that consumes the result of the operation that is actually slow. Do you actually need to take the square root of both of them, or can you do the selection on the squared distances?

Failing that, (1) turn off thumb when targeting ARMv6 and (2) use __builtin_fminf(distanceA, distanceB). Then move on to finding the real bottlenecks in your app; 60 times per second is nothing to worry about.

Stephen Canon