我该如何改进这段代码?

时间:2022-11-12 03:59:31

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

Shark告诉我,这是糟糕表现的热点:

double distanceA = fabsf(target - current);

target is CGFloat current is CGFloat

目标是CGFloat当前是CGFloat

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

我的其余计算依赖于双倍。也许有一些更聪明的方法可以做到这一点?

2 个解决方案

#1


0  

Not near Xcode right now, but try:

目前不在Xcode附近,但请尝试:

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

#2


6  

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.

除非您需要距离的双倍精度,否则请更改浮动距离。 CGFloat被定义为iPhone上的浮点数,fabsf()接受浮点值并返回浮点值。处理双精度值时,iPhone的标准浮动速度要慢得多。

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.

事实上,对于你的距离变量使用双精度是没有意义的,因为你已经通过fabsf()运行计算而失去了精度,而fabsf()只返回一个浮点数。

#1


0  

Not near Xcode right now, but try:

目前不在Xcode附近,但请尝试:

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

#2


6  

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.

除非您需要距离的双倍精度,否则请更改浮动距离。 CGFloat被定义为iPhone上的浮点数,fabsf()接受浮点值并返回浮点值。处理双精度值时,iPhone的标准浮动速度要慢得多。

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.

事实上,对于你的距离变量使用双精度是没有意义的,因为你已经通过fabsf()运行计算而失去了精度,而fabsf()只返回一个浮点数。