Re: IOS floating point performance
Re: IOS floating point performance
- Subject: Re: IOS floating point performance
- From: David Rowland <email@hidden>
- Date: Thu, 08 Aug 2013 10:26:01 -0700
I wrote an app that calculates the positions of Sun and Moon and other information as well. The heart of the Moon calculation is this. I added a loop around it and called a stopwatch at the beginning and end.
startTime();
for (int jj = 0; jj<100;++jj)
{
//in radians
double lambda = 3.81040282295402 + 8399.70910754626 * T
+ 0.109781209950443 * sin(adjustValueRadians(2.35619449019234 + 8328.69146829639 * T))
- 0.022165681500328 * sin(adjustValueRadians(4.5256387504213 - 7214.06294691607 * T))
+ 0.0115191730631626 * sin(adjustValueRadians(4.11374104695063 + 15542.7542406795 * T))
+ 0.00366519142918809 * sin(adjustValueRadians(4.7106436511327 + 16657.3829365928 * T))
- 0.00331612557878923 * sin(adjustValueRadians(6.23955207587973 + 628.301950090065 * T))
- 0.00191986217719376 * sin(adjustValueRadians(3.25503905496942 + 16866.9322280421 * T));
lambda = adjustValueRadians(lambda);
double beta =
+ 0.0895353906273091 * sin(adjustValueRadians(1.62839219211071 + 8433.46620128749 * T))
+ 0.00488692190558412 * sin(adjustValueRadians(3.98284135305106 + 16762.1576695839 * T))
- 0.00488692190558412 * sin(adjustValueRadians(5.55538300909795 + 104.774732991098 * T))
- 0.00296705972839036 * sin(adjustValueRadians(3.79783645233966 - 7109.28821392497 * T));
pi = 0.0165945905279621
+ 0.000904080552533063 * cos(2.35619449019234 + 8328.69146829639 * T)
+ 0.000165806278939461 * cos(4.5256387504213 - 7214.06294691607 * T)
+ 0.000136135681655558 * cos(4.11374104695063 + 15542.7542406795 * T)
+ 0.0000488692190558412 * cos(4.7106436511327 + 16657.3829365928 * T);
//convert to equatorial
moonEclipticCoordinates.latitude = beta;
moonEclipticCoordinates.longitude = lambda;
}
endTime();
One hundred times through this loop, on my iPhone 5, took about 0.0028 seconds. Two hundred times took about 0.0056 sec.
I infer that one pass takes about 0.000028 seconds, or 28.0 microseconds.
The functions are probably very carefully written and could not be improved by table lookups, vector libraries, etc. That is barking up the wrong tree.
David
On Aug 8, 2013, at 8:30 AM, Thomas Wetmore <email@hidden> wrote:
> Fritz,
>
> I know you know that the accuracy of this approach goes far
> beyond being accurate only on the half degrees. The interpolation,
> which can be done by very simple linear interpolation, will
> convey almost the same level of accuracy on all intervening
> angle values. There are some places where the trig function reach
> zeros and infinities, and these have to be handled with care, but
> it isn't rocket science.
>
> Note you never have to call tan() since tan = sin/cos, though
> you do have to worry about when cos(α) is zero. (Don't have
> to call any other trig function either).
>
> Tom Wetmore
>
> On Aug 8, 2013, at 11:19 AM, Fritz Anderson <email@hidden> wrote:
>
>> And if half-degrees are too coarse for you, you can take advantage of the cyclic nature of the derivatives of sine and cosine, and run the Taylor series out as far as you like (though you'd probably lose out to a professionally-crafted trig library pretty quickly). I guess that could be vectorized, but it's one more thing to debug. Profile the Accelerate.framework functions first.
>>
>> — F
>>
>> On 8 Aug 2013, at 10:01 AM, Thomas Wetmore <email@hidden> wrote:
>>
>>> p.s. Of course you don't have to call sin() and cos() for
>>> every half degree when building these tables. You can take advantage
>>> of how trig functions repeat in the four quadrants, and you can
>>> take advantage of other inverse and pythagorean reationships
>>> that exist between them. Even the initial table building
>>> can be optimized.
>>>
>>> Tom Wetmore
>>>
>>> On Aug 8, 2013, at 10:53 AM, Thomas Wetmore <email@hidden> wrote:
>>>
>>>> Returning strictly to the issue of trig performance. A solution I
>>>> have used in the past is to initialize tables of trig functions,
>>>> say by calling sin() and cos() for every half a degree, and then
>>>> interpolating those tables, never calling sin() or cos() again.
>>>> I did this 29 years ago on an Atari 520ST to simulate the solar
>>>> system, and it worked well. You only need enough trig accuracy
>>>> for graphics to plot to the correct pixels. It takes very few
>>>> decimal places to get that accuracy.
>>>>
>>>> Tom Wetmore
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden