How can I improve this piece of code?

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?

Upvotes: 1

Views: 232

Answers (2)

Brad Larson
Brad Larson

Reputation: 170317

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.

Upvotes: 6

Chris Long
Chris Long

Reputation: 3084

Not near Xcode right now, but try:

double distanceA = (double) fabsf(target - current);

Upvotes: 0

Related Questions