• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Performance of calculation on float point numbers in a array • NSNumber in NSArray, or classic C array?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Performance of calculation on float point numbers in a array • NSNumber in NSArray, or classic C array? (From: Stanislas Krásnaya <email@hidden>)
 >Re: Performance of calculation on float point numbers in a array • NSNumber in NSArray, or classic C array? (From: Quincey Morris <email@hidden>)
 >Re: Performance of calculation on float point numbers in a array • NSNumber in NSArray, or classic C array? (From: Stanislas Krásnaya <email@hidden>)

  • Prev by Date: Re: CLLocationDistance contents?
  • Next by Date: Re: download security hanging app
  • Previous by thread: Re: Performance of calculation on float point numbers in a array • NSNumber in NSArray, or classic C array?
  • Next by thread: Re: Performance of calculation on float point numbers in a array • NSNumber in NSArray, or classic C array?
  • Index(es):
    • Date
    • Thread