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: Martin Wierschin <email@hidden>
- Date: Thu, 18 Dec 2008 19:23:59 -0800
If I have to optimise something later, I will do it.
You can take advantage of Objective-C's categories to make future
optimization an easier task. For example, rather than coding:
for( unsigned ii = 0; ii < count; ii++ ) {
sum += [[array objectAtIndex:ii] floatValue];
}
Instead do:
@interface NSArray (MYStatsAdditions)
- (float) floatAtIndex:(unsigned)idx ;
@end
@implementation NSArray (MYStatsAdditions)
- (float) floatAtIndex:(unsigned)idx
{
return [[self objectAtIndex:idx] floatValue];
}
@end
for( unsigned ii = 0; ii < count; ii++ ) {
sum += [array floatAtIndex:ii];
}
Then later you can just pop in a custom NSObject subclass that
implements "floatAtIndex" backed by a plain C array without changing
any other code. You could even take this a step further by (perhaps
abusing) categories to add:
@interface NSArray (MYStatsAdditions)
- (float) floatAtIndex:(unsigned)idx ;
- (float) floatSum ;
- (float) floatMedian ;
// etc
@end
At that point it would probably be worth creating a custom object
(eg: MYStatsVector), initially backed by a normal NSArray and only
changing the guts to a plain C array later if needed.
~Martin
_______________________________________________
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