On Aug 7, 2013, at 2:27 PM, Fritz Anderson wrote:

> On 7 Aug 2013, at 3:50 PM, Trygve Inda <cocoa...@xericdesign.com> wrote:
> 
>> I have an app that is running slow. I have narrowed it down to several
>> functions which are trig-intensive (used to calculate the position of the
>> moon at a given moment and more specifically to calculate rise/set times).
>> 
>> To calculate the position and rise/set times for a month on average,
>> requires:
>> 
>> 300 trig functions for position
>> 24 points in time to find the correct set of three points
>> 30 days
>> 2 events
>> 
>> Total of 432,000 trig functions (mostly sin and cos)
>> 
>> On my iPad 3, this takes about 3 seconds, and on my desktop (2.3GHz
>> Quad-core i7) it is nearly instantaneous.
>> 
>> I am using doubles (not floats).
>> 
>> What can I do to speed this up?
> 
> You don't have to be told that desktop performance with the same algorithm on 
> a processor that is 1-10% as fast isn't going to happen. 
> 
> If you can figure out how to vectorize your calculations, look at veclib in 
> the Accelerate framework. vvsin() and vvsinf() have been available since iOS 
> 5.
> 
> I'm a little surprised to see that veclib supports doubles. My instinct 
> (based on imagining that you'll be striding through an array with vector 
> registers that can hold two doubles or four floats)* is that floats could be 
> much faster, and you should really think about whether you need doubles: 
> You're depicting the moon, not flying there.
> 
> * (Readers should consider whether correcting my rule-of-thumb notion is a 
> good use of their time.)
> 
>       — F

You can also use lookup tables for your trig functions, especially if you can 
live with less precision.

And you can look and see if there are calculations that are going to be 
repeated, and do those only once (or once per loop, as appropriate).

Also, you might be able to combine some of the calculations into simpler ones, 
using the rules of trig (think sin2x, etc.).

Another approach is to represent sines and cosines as infinite series, and 
combine those series in order to compute one (or two, or a few) series that you 
can then either compute directly (using finite approximations to the infinite 
series) or turn them back into trig functions.  (That's difficult work, though, 
far beyond just using trig functions.)

-Howard


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to