I’m running time profiler in instruments. I’ve simplified the code as much as I can to boil everything down to the exact issue. The line of code inside the loop that has checkInString =[_formatter stringFromDate:[checkInArrayCopy objectAtIndex:i]]; is taking over 90% of the processing time. Any ideas on how I can optimize this code?
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
[format setDateFormat:@"MM/dd/YYYY"];
NSString *checkInString;
for (int x=0; x<100; x++) {
for (int i=0; i<checkInArray.count; i++) { //CheckInArray is a NSMutableArray of NSDates, with about 100 objects inside
checkInString =[_formatter stringFromDate:[checkInArray objectAtIndex:i]]; //**90% of processing time
}
}
To be honest, I think any big improvements are going to be algorithmic changes that are beyond the scope of what we can practically suggest here (e.g. reduce the amount of looping you need to do, or eliminate the need to get all the dates’ strings).
There are a couple of micro-optimizations you could do, though I don’t expect them to make a drastic difference. Basically, you can reduce your number of message sends by using IMP caching and NSArray’s enumeration methods instead of a C for-loop, which should give a small boost.
(Written in browser, so caveat compilor.)