Re: Performance of calculation on float point numbers in a array • NSNumber in NSArray, or classic C array?
Re: Performance of calculation on float point numbers in a array • NSNumber in NSArray, or classic C array?
- Subject: Re: Performance of calculation on float point numbers in a array • NSNumber in NSArray, or classic C array?
- From: "I. Savant" <email@hidden>
- Date: Thu, 18 Dec 2008 14:27:01 -0500
Stanislas:
> I've read that, since we're programming in Objective-C, it's more elegant
> and reliable to use an NSArray stuffed with NSNumber instead of a classic C
> array. But what I want to do, it's performing classic calculation, like mean
> value, variance... etc, on floating point numbers. Typically around 1000 of
> them.
>
> I know that our computers are fast now, but this doesn't mean that we have
> to not take care of optimization.
I faced this very same situation with a scientific data mining and
visualization application. In my case, it was a permutation test
against a whole lot of doubles (for reasons of precision). When
dealing with any level of precision, you may have to "roll the dice"
10,000 times per comparison or more. Because the application takes
advantage of Core Data and (to an extent Cocoa Bindings) I first
implemented this using NS[Mutable]Arrays of NSNumbers.
It did not perform well. :-)
The standard test set I used took literally around a minute to run.
Unacceptable for a "explore as you go" visualization UI. I then asked
myself why I felt everything had to be shoehorned into Cocoa /
Objective-C ... of course I was embarrassed and had no explanation to
offer myself. After I'd thoroughly chastised myself for being so
ridiculous, I re-implemented the test by extracting the necessary
values into a C array of doubles (and another of randomized vector
indices) then doing the calculations.
First, I'll say that this felt wasteful because "they're already
neatly-packaged objects - won't performance suffer copying values back
and forth like that?" Once I got over that and actually reasoned it
out, I informed myself that the answer was, of course, "no, because
comparatively speaking, copying the values around is nothing next to
100,000 iterations and many more sufficiently-random-number
generations to shuffle the vectors".
To which I replied, "Oh." :-)
All told, the minute-long permutation test was reduced to about two
seconds. Wow.
I hope this is helpful.
--
I.S.
_______________________________________________
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