Q:

NSDate processing very CPU intensive

I have a method in my code for processing a string that needs to be transformed in a NSDate. 
This method is called hundreds on times a second, and apparently is very inefficient. 
How can it be improved as much as possible? 
- (NSDate *)getDateFromString:(NSString *)dateString
{
    NSDateFormatter* formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

    NSDate* date = [formatter dateFromString:dateString];

    if (date == nil) {
        [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
        date = [formatter dateFromString:dateString];
    }
    return date;
}

A:

The number one thing you can do is don't create that formatter instance each call!
Creating formatters are very expensive. From Apple's "Data Formatting Guide":

Creating a date formatter is not a cheap operation. If you are likely to use a formatter frequently, it is typically more efficient to cache a single instance than to create and dispose of multiple instances.

You can do this with a static var or with properties and the lazy load pattern. For example, using static:
-(NSDate *)getDateFromString:(NSString *)dateString{
  static NSDateFormatter *formatterWithZone = nil;
  static NSDateFormatter *formatterWithoutZone = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    formatterWithZone = [NSDateFormatter new];
    [formatterWithZone setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    [formatterWithZone setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    formatterWithoutZone setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
  });

  NSDate *date = [formatterWithZone dateFromString:dateString];
  //...
  date = [formatterWithoutZone dateFromString:dateString];
}

Or, if you're going to use the formatter elsewhere, put it in a property:
@property (nonatomic) NSDateFormatter *formatterWithZone;
@property (nonatomic) NSDateFormatter *formatterWithoutZone;
//...

-(NSDateFormatter *)formatterWithZone{
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    _formatterWithZone = [NSDateFormatter new];
    [_formatterWithZone setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    [_formatterWithZone setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
  });
  return _formatterWithZone;
}

-(NSDateFormatter *)formatterWithoutZone{
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    _formatterWithoutZone = [NSDateFormatter new];
    [_formatterWithoutZone setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
  });
  return _formatterWithoutZone;
}

-(NSDate *)getDateFromString:(NSString *)dateString{
  NSDate* date = [[self formatterWithZone] dateFromString:dateString];
  //...
  date = [[self formatterWithoutZone] dateFromString:dateString];
}

UPDATE:
Also check out (and upvote) mrueg's comment below about NSCalendar -currentCalendar being expensive. Both the above techniques would work to cache an NSCalendar instance as well.

A:

Date formatters are very expensive to create (and to change the format of). 
Make two static date formatter variables, one for each format, and use them instead of creating new formatters each time. 
Create the formatters inside a dispatch_once block. 

