views:

126

answers:

2

Shark tells me that this is a hotspot for bad performance:

double distanceA = fabsf(target - current);

target is CGFloat current is CGFloat

the rest of my calculations rely on double. Maybe there's some smarter way to do this anyways?

A: 

Not near Xcode right now, but try:

double distanceA = (double) fabsf(target - current);
Chris Long
I think that helped a little bit to improve performance. thank you.
Another Registered User
I would think that that just makes explicit what implicitly was already occurring (a cast from float to double)? How would that improve anything? (not snarky, I don't know a thing about cocoa)
Mikeb
Haha, I don't really know. IIRC, Xcode doesn't like it when you don't cast a value explicitly. Maybe this set off a flag in Shark. That was really the only (pseudo-) fault I could think of.
Chris Long
I can't see how that changes anything. The compiler already has to convert it to double for the assignment even without the cast.
progrmr
+5  A: 

Unless you have a need for double precision in your distance, change distance to a float. CGFloat is defined as a float on the iPhone, and fabsf() takes a float value and returns a float value. The iPhone can be significantly slower at handling double-precision values than standard floats.

In fact, there's no point in using double precision for your distance variable, because you have already lost precision by running your calculation through fabsf(), which only returns a float.

Brad Larson